Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 246
Blankman Avatar asked Nov 28 '22 05:11

Blankman


1 Answers

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.

like image 99
lucasrizoli Avatar answered Nov 29 '22 19:11

lucasrizoli