I have a numeric value like 30.6355 that represents money, how to round to 2 decimal places?

You should not use double or float types when dealing with currency : they have both too many decimal places and occasional rounding errors. Money can fall through those holes and it'll be tough to track down the errors after it happens.

Up vote 2 down vote favorite share g+ share fb share tw.

Ruby decimal rounding link|improve this question edited Nov 9 '10 at 4:25Mike Trpcic7,7721844 asked Nov 9 '10 at 4:20Blankman13.2k2498255 60% accept rate.

– Nakilon Nov 9 '10 at 4:27 @Jason Because many people that deal with money (need to) keep values much more precise than the average consumer...? – deceze Nov 9 '10 at 4:27 1 After computing tax or discounts, >2 decimals are quite common... – Michael Haren Nov 9 '10 at 4:36.

You should not use double or float types when dealing with currency: they have both too many decimal places and occasional rounding errors. Money can fall through those holes and it'll be tough to track down the errors after it happens. When dealing with money, use a fixed decimal type.

In Ruby (and Java), use BigDecimal.

6 Actually, the biggest problem is that they don't have any decimal places at all, they have binary places. – Jörg W Mittag Nov 9 '10 at 5:09 @Jörg, is right. See en.wikipedia.org/wiki/Floating_point#Acc... for an explanation.

– lucasrizoli Nov 9 '10 at 5:59 gem install money. Money.rubyforge.org – Lars Haugseth Nov 9 '10 at 9:18 but Ruby doesn't have types? How can I make it bigdecimal?

– Blankman Nov 9 '10 at 16:36.

Ruby 1.8: class Numeric def round_to( places ) power = 10.0**places (self * power). Round / power end end (30.6355). Round_to(2) Ruby 1.9: (30.6355).

Round(2) In 1.9, round can round to a specified number of digits.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions