Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/lafcadio/util.rb

Instance Method Summary collapse

Instance Method Details

#precisionFormat(precision, padDecimals = true) ⇒ Object

Returns a string that represents the numbeer to precision decimal places, rounding down if necessary. If padDecimals is set to false and the number rounds to a whole number, there will be no decimals shown.

(24.55).precisionFormat( 3 )    -> "24.550"
(24.55).precisionFormat( 0 )    -> "24"
100.precisionFormat( 2, false ) -> "100"


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lafcadio/util.rb', line 53

def precisionFormat(precision, padDecimals = true)
  str = floor.to_s
  if precision > 0
    decimal = self - self.floor
    decimalStr =(decimal * 10 ** precision).floor.to_s
    if decimalStr.size < precision
      decimalStr += "0" *(precision - decimalStr.size)
    end
    if padDecimals || decimalStr =~ /[123456789]/
     str += "."
	    str += decimalStr
	  end
  end
  str
end