ruby - How to specify the precision params with a String in BigDecimal constructor? -
the bigdecimal
constructor take optional second params specify precision digits of object. ruby-doc:
new(initial, digits)
the number of significant digits, fixnum. if omitted or 0, number of significant digits determined initial value.
however, when working string, behavior doesn't match description.
bigdecimal.new('1.2345', 4).to_s('f') # 1.2345 bigdecimal.new('1.2345', 1).to_s('f') # 1.2345
how specify precision bigdecimal when working string param?
bigdecimal not take precision param string value.
right approach:
bigdecimal.new("1020.567").round(2) => 1020.57
wrong approach:
bigdecimal.new(1020.567, 2) => 1000.0
Comments
Post a Comment