inconsistent output from Float#to_s

J

Jeff Mitchell

irb(main):002:0> 5.0.to_s
=> "5.0"
irb(main):003:0> 5.000000000000007.to_s
=> "5.00000000000001"
irb(main):004:0> 5.0000000000000007.to_s
=> "5"
irb(main):005:0> 5.00000000000000007.to_s
=> "5.0"

I came across this while printing the output of a function across
a range of values. I was befuddled as to why ruby would not print
a decimal point for certain float values.

Is there some rationale behind it? I expected a stringified Float
to always contain a decimal point.





__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top