Class: TaskJuggler::RealFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/taskjuggler/RealFormat.rb

Overview

This class provides the functionality to format a Float according to certain rules. These rules determine how negative values are represented, how the fractional part is shown and how to structure the mantissa. The result is always a String.

The class uses the following parameters to control the formating. signPrefix: Prefix used for negative numbers. (String) signSuffix: Suffix used for negative numbers. (String) thousandsSeparator: Separator used after 3 integer digits. (String) fractionSeparator: Separator used between the inter part and the

fractional part. (String)

fractionDigits: Number of fractional digits to show. (Integer)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RealFormat

Create a new RealFormat object and define the formating rules.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/taskjuggler/RealFormat.rb', line 34

def initialize(args)
  iVars = %w( @signPrefix @signSuffix @thousandsSeparator
              @fractionSeparator @fractionDigits )
  if args.is_a?(RealFormat)
    # The argument is another RealFormat object.
    iVars.each do |iVar|
      instance_variable_set(iVar, args.instance_variable_get(iVar))
    end
  elsif args.length == 5
    # The argument is a list of values.
    args.length.times do |i|
      instance_variable_set(iVars[i], args[i])
    end
  else
    raise RuntimeError, "Bad number of parameters #{args.length}"
  end
end

Instance Attribute Details

#fractionDigitsObject (readonly)

Returns the value of attribute fractionDigits.



30
31
32
# File 'lib/taskjuggler/RealFormat.rb', line 30

def fractionDigits
  @fractionDigits
end

#fractionSeparatorObject (readonly)

Returns the value of attribute fractionSeparator.



30
31
32
# File 'lib/taskjuggler/RealFormat.rb', line 30

def fractionSeparator
  @fractionSeparator
end

#signPrefixObject (readonly)

Returns the value of attribute signPrefix.



30
31
32
# File 'lib/taskjuggler/RealFormat.rb', line 30

def signPrefix
  @signPrefix
end

#signSuffixObject (readonly)

Returns the value of attribute signSuffix.



30
31
32
# File 'lib/taskjuggler/RealFormat.rb', line 30

def signSuffix
  @signSuffix
end

#thousandsSeparatorObject (readonly)

Returns the value of attribute thousandsSeparator.



30
31
32
# File 'lib/taskjuggler/RealFormat.rb', line 30

def thousandsSeparator
  @thousandsSeparator
end

Instance Method Details

#format(number) ⇒ Object

Converts the Float number into a String representation according to the formating rules.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/taskjuggler/RealFormat.rb', line 54

def format(number)
  # Check for negative number. Continue with the absolute part.
  if number < 0
    negate = true
    number = -number
  else
    negate = false
  end

  # Determine the integer part.
  intNumber = (number * (10 ** @fractionDigits)).round.to_i.to_s
  if intNumber.length <= @fractionDigits
    intNumber = '0' * (@fractionDigits - intNumber.length + 1) + intNumber
  end
  intPart = intNumber[0..-(@fractionDigits + 1)]
  # Determinate the fractional part
  fracPart =
    @fractionDigits > 0 ? @fractionSeparator +
                          intNumber[-(@fractionDigits)..-1] : ''

  if @thousandsSeparator.empty?
    out = intPart
  else
    out = ''
    1.upto(intPart.length) do |i|
      out = intPart[-i, 1] + out
      out = @thousandsSeparator + out if i % 3 == 0 && i < intPart.length
    end
  end
  out += fracPart
  # Now compose the result.
  out = @signPrefix + out + @signSuffix if negate
  out
end

#to_sObject



89
90
91
92
# File 'lib/taskjuggler/RealFormat.rb', line 89

def to_s
  [ @signPrefix, @signSuffix, @thousandsSeparator, @fractionSeparator,
    @fractionDigits ].collect { |s| "\"#{s}\"" }.join(' ')
end