Module: Humanized::Number

Defined in:
lib/humanized/interpolation/number.rb

Defined Under Namespace

Classes: PartitionEnumerator

Instance Method Summary collapse

Instance Method Details

#number(humanizer, number, format = 'default', precision = '') ⇒ Object



53
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
88
89
90
91
92
93
94
# File 'lib/humanized/interpolation/number.rb', line 53

def number(humanizer, number, format = 'default', precision='')
  
  if format == 'default' or format.nil?
    it = number._(:format,:default)
  else
    format = format.to_sym
    it = number._.format( format._ | :default._ )
  end
  
  if precision.kind_of? String and precision.length > 0
    precision = x_to_i(humanizer,precision)
  end
  
  unless precision.kind_of? Integer
    precision = humanizer.get( it.precision , :default=>0 )
  end
  
  num = number.round_at(precision).to_s
  full, frac = num.split('.', 2)
  
  if( full.length > 3 )
    separator = humanizer.get( it.separator , :default=>'' )
    if separator.length > 0
      full = PartitionEnumerator.new(0...full.length, 3).map{|rng|
        full[rng]
      }.join(separator)
    end
  end
  
  if( precision > 0 )
    delimiter = humanizer.get( it.delimiter , :default=>'.' )
    if frac.length > precision
      frac = frac[0...precision]
    else
      frac = frac.ljust(precision, '0')
    end
    return [full, frac].join(delimiter)
  end
  
  return full
  
end