-1

I know that in Java we have a lot of methods to round, but if my code returns the value

0.19998344639311716

And I need the value

0.2

If I use the method Math.round() it will return 0.0

If I use Math.ceil() it will return 1.0

What method I can use to return 0.2?

Rikkin
  • 509
  • 1
  • 5
  • 20
  • If your code has those errors and they matter you probably should not be using floating point. Check out BigDecimal. – Diego Basch Oct 01 '14 at 19:04
  • http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Grice Oct 01 '14 at 19:06

2 Answers2

1

Well, you can always Math.round(x*10) / 10 .

zmbq
  • 38,013
  • 14
  • 101
  • 171
0

Here's a big decimal example:

BigDecimal bd = new BigDecimal(double_value).setScale(1, RoundingMode.HALF_EVEN);
ne1410s
  • 6,864
  • 6
  • 55
  • 61