|v1=3D"128.015"
|puts((v1.to_f*1000).to_i).to_s // giving 128014 instead of 128015
|
|Why the the result is rounding up to 128014 but the correct result
|should be 128015.
I'm not an expert, but I think because when you write 128.015, the number y=
ou=20
actually get is 128.0149999999999863575... (because 128.015 can't be=20
represented as a number with a finite number of decimal digits in binary, j=
ust=20
like, for example, 1/17 can't be repsented with a finite decimal number in=
=20
decimal). You usually don't see this because by default only a small number=
of=20
decimal digits are shown, taking rounding into account. You can obtain a=20
greater number of digits using, for example, format:
puts format("%.20f", 128.015)
=3D> 128.01499999999998635758
Now, when you multiply the number by 1000, what you actually get is:=20
128014.99999999998544808477. And, since Float#to_i truncates the number (it=
=20
doesn't round it), you get the number 128014.
I hope this helps
Stefano