Does anybody has solution for this problem in ruby :
let say we have : a = 8.1999999
We wanted to round it by 2 decimal which is 8.20 and multiply it by 1,000,000 to become 8,200,000
We do it this way ;
(a.round(2) * 1000000).to_i
But what we got is 8199999, why?
The strage things is, we got the correct result if we multiply by 1000, 100000, or 10000000, but not 1000000. Any body know why?
We are using ruby 1.9.2 and try with 1.9.3 as well.
Thanks!
The result of multiplying a float and an integer is always going to be a float. Copied! You can use the round() function if you need to round the result to N digits precision after the decimal point.
Example: Multiply Two Floating-Point Numbers This ensures the numbers are float , otherwise they will be assigned - type double . first and second are then multiplied using the * operator and the result is stored in a new float variable product .
In other words, a float in a Ruby program is a number that contains a decimal point. Ruby will consider any number written without decimals as an integer (as in 138 ) and any number written with decimals as a float (as in 138.0 ).
Whenever you get funky numbers in calculations use bigdecimal
require 'bigdecimal'
a = BigDecimal(8.1999999.to_s)
(a.round(2) * 1000000).to_i
It becomes like that because a.round(2) returns a floating point number, thus calculations are not perfect.
For correct result, try following: (10*a).round.to_i * 100000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With