I have a numerical value like 30.6355, which represents money, how to round to two decimal places?

I have a numerical value like 30.6355, which represents money, how to round to two decimal places?

+4
source share
3 answers

You should not use double or float types when working with currencies : they have too many decimal places and random rounding errors. Money can fall through these holes, and after that it will be difficult to track errors.

When working with money, use a fixed decimal type. In Ruby (and Java), use BigDecimal.

+20
source

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 certain number of digits.

+13
source

It will be round for some useful occasions - poorly written, but it works! Feel free to edit.

 def round(numberString) numberString = numberString.to_s decimalLocation = numberString.index(".") numbersAfterDecimal = numberString.slice(decimalLocation+1,numberString.length-1) numbersBeforeAndIncludingDeciaml = numberString.slice(0,decimalLocation+1) if numbersAfterDecimal.length <= 2 return numberString.to_f end thingArray = numberString.split("") thingArray.pop prior = numbersAfterDecimal[-1].to_i idx = numbersAfterDecimal.length-2 thingArray.reverse_each do |numStr| if prior >= 5 numbersAfterDecimal[idx] = (numStr.to_i + 1).to_s unless (idx == 1 && numStr.to_i == 9) prior = (numStr.to_i + 1) else prior = numStr.to_i end break if (idx == 1) idx -= 1 end resp = numbersBeforeAndIncludingDeciaml + numbersAfterDecimal[0..1] resp.to_f end 

round(18.00) == 18.0

round(18.99) == 18.99

round(17.9555555555) == 17.96

round(17.944444444445) == 17.95

round(15.545) == 15.55

round(15.55) == 15.55

round(15.555) == 15.56

round(1.18) == 1.18

round(1.189) == 1.19

0
source

All Articles