Truncating Floats

K

Kero

Probably cause you can do it in one line:
('%.2f' % 0.255).to_f

That's rounding, not truncating.

For some purposes, there's BigDecimal, where x.yz is really two decimals
(and not an approximation as a flost takes)

And behold! BigDecimal#floor takes an argument, the number of digits you
want (had to look that up myself; learn something new every day :)

+--- Kero ------------------------- kero@chello@nl ---+
| all the meaningless and empty words I spoke |
| Promises -- The Cranberries |
+--- M38c --- http://members.chello.nl/k.vangelder ---+
 
S

soxinbox

for more enjoyment and greater efficiency:
(s=2.33333.to_s + "00").slice(0,s.index(".")+3)
 

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
474,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top