views:

1665

answers:

4

Sort of a quick question. I'm writing:

puts "%.3f %.4f %.5f" % [3.998877, 3.998877, 3.998877]

and get the following output:

3.999 3.9989 3.99888

sprintf simply rounds the numbers. How do I restrict that rounding?

+1  A: 

you will probably need to truncate the numbers to the accuracy that you want.

f = 3.1919183
puts (f * 1000).truncate() / 1000
CookieOfFortune
sorry? could you give an example?
gmile
I'm not sure what you mean by "restrict the rounding". Specifically, what result are you looking for in your above example?
Jeff Godfrey
I just want my numbers 'as is': 3.998 3.9988 3.99887
gmile
so that would be truncation.
rampion
A: 

To get the numbers 'as is', you should store them as strings. As soon as you represent them as floats you lose information about the amount of built-in precision.

Peter
Why not store them as rational numbers instead?
Robert L
depends what you want to do with them later, I guess.
Peter
+1  A: 
>> def truncN f, digits
>>    t = 10.0 ** digits
>>    "%.#{digits}f" % ((f * t).truncate / t)
>> end
=> nil
>> n
=> 1.11181111
>> "%.3f" % n
=> "1.112"
>> truncN n, 3
=> "1.111"
DigitalRoss
+1  A: 
>> 3.998877.to_s[/^\d+\.\d{3}/].to_f
=> 3.998
>> 3.998877.to_s[/^\d+\.\d{4}/].to_f
=> 3.9988
Not bad solution
gmile