• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a class that can do a custom Math.round()  RSS feed

 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a problem to solve in the interview which said that
Digits should be rounded off to a precision level of 0.05
.

Now i tried out the math formula to do that using Double as the object, but unfortunately, math bug crept in and the resulting value became something like 12.150000000002. Now i have no option but to use the Decimal format.. ..

Just wanted to ask is there another way out??

Regards,
Rohit
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doubles are inherently imprecise. If you require exact *numbers*, then BigDecimal is the way to go. But if you require an exact *textual* representation of the rounded number, then the java.text.DecimalFormat class can help.
 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot sir...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!