3

I'm still new to Java and I was wondering if there are any ways to format to a double without having it rounded? Example:

double n = 0.12876543;
String s = String.format("%1$1.2f", n);

If I were to print to the system, it would return the 0.13 instead of the precise 0.12. Now I have thought of a solution but I want to know if there is a better way of doing this. This my simple solution:

double n = 0.12876543;
double n =  Double.parseDouble(String.format(("%1$1.2f", n));

Any other thoughts or solutions?

ItamarG3
  • 4,092
  • 6
  • 31
  • 44
NewBie1234
  • 461
  • 6
  • 23
  • 3
    Don't format it to two decimal place and it won't get formatted to two decimal places. How is `0.12` more precise than `0.13` when the original number is `0.128...`? – Boris the Spider Oct 28 '16 at 13:43
  • Use [`Math.floor()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html): `n = Math.floor(n * 100) / 100;`. – Phylogenesis Oct 28 '16 at 13:43
  • Sounds like you want to set the rounding mode in String.format, which you can;t do. Maybe use BigDecimals instead? See answers to http://stackoverflow.com/questions/26644853/java-string-format-with-half-even-rounding – BretC Oct 28 '16 at 13:45
  • Or you can use `String.format("%1$1.2f", n - 0.005);`. – Phylogenesis Oct 28 '16 at 13:48
  • This is for a french user, they use the coma, which is why I used the format which recognizes the machine you work on. Its for a pay system, if you had up all the numbers all the other digits do make a difference which is why I don't want to round them up. I could had .replace(".", ",") to get the coma in this instance. – NewBie1234 Oct 28 '16 at 13:48

4 Answers4

4

An elegant solution would be to use setRoundingMode with DecimalFormat. It sets the RoundingMode appropriately.

For example:

// Your decimal value
double n = 0.12876543;
// Decimal Formatting
DecimalFormat curDf = new DecimalFormat(".00");
// This will set the RoundingMode
curDf.setRoundingMode(RoundingMode.DOWN);
// Print statement
System.out.println(curDf.format(n));

Output:

0.12

Further, if you want to do additional formatting as a string you can always change the double value into string:

// Your decimal value
double n = 0.12876543;
// Decimal Formatting
DecimalFormat curDf = new DecimalFormat(".00");
// This will set the RoundingMode
curDf.setRoundingMode(RoundingMode.DOWN);
// Convert to string for any additional formatting
String curString = String.valueOf(curDf.format(n));
// Print statement
System.out.println(curString);

Output:

0.12

Please refer similar solution here: https://stackoverflow.com/a/8560708/4085019

Community
  • 1
  • 1
PseudoAj
  • 5,234
  • 2
  • 17
  • 37
2

As is, rounded to 2 decimals and truncated to 2 decimals :

double n = 0.12876543;
String complete = String.valueOf(n);
System.out.println(complete);

DecimalFormat df = new DecimalFormat("#.##");
String rounded = df.format(n);
System.out.println(rounded);

df.setRoundingMode(RoundingMode.DOWN);
String truncated = df.format(n);
System.out.println(truncated);

it displays :

0.12876543
0.13
0.12
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
1

Your example is working correctly in that it is properly rounding the number to 2 decimal places. 0.12876543 properly rounds to 0.13 when rounded to 2 decimal places. However, it seems like you always want to round the number down? If that is the case then you can do something like this...

public static void main(String[] args) throws IOException, InterruptedException {
    double n = 0.12876543;

    DecimalFormat df = new DecimalFormat("#.##");
    df.setRoundingMode(RoundingMode.DOWN);
    String s = df.format(n);
    System.out.println(s);
}

This will print out a value of 0.12

Justin L
  • 375
  • 2
  • 10
0

Note first that a double is a binary fraction and does not really have decimal places.

If you need decimal places, use a BigDecimal, which has a setScale() method for truncation, or use DecimalFormat to get a String.

M Zippka
  • 93
  • 13