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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -