1

I am building a class that will be required to round doubles.

In it, based on input n, it will (at max) have n decimal places and skip trailing zeroes if possible.

//If n = 2,

//1.697 -> 1.7  (instead of 1.70)     (edit)       
//1.000 -> 1    (instead of 1.00)             
//1.658 -> 1.66

There seems to be numerous rounding techniques but I'm not sure which one is applicable for my case.

Any help would be appreciated. Thank you

user2999870
  • 333
  • 3
  • 10
  • 3
    Why would 1.693 be rounded to 1.7 and not to 1.69? – PendingValue Apr 14 '16 at 13:39
  • 1
    Possible duplicate of [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) –  Apr 14 '16 at 13:40
  • The examples are RoundingMode.UP which is not the norm RoundingMode.HALF_UP: the superfluous digit being 5-9 rounding up, 0-4 rounding down. See https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html – Joop Eggen Apr 14 '16 at 13:43
  • Note: `1 == 1.00` the values is the same, the only difference is how you choose to format it. – Peter Lawrey Apr 14 '16 at 13:56

1 Answers1

3

You should read about DecimalFormatting here: https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

Although your question is a bit confusing. Why round 1.693 to 1.7 but then 1.658 to 1.66. You're rounding different in both.

primelf
  • 336
  • 4
  • 16