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?

5
  • 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...? Commented Oct 28, 2016 at 13:43
  • Use Math.floor(): n = Math.floor(n * 100) / 100;. Commented Oct 28, 2016 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 stackoverflow.com/questions/26644853/… Commented Oct 28, 2016 at 13:45
  • Or you can use String.format("%1$1.2f", n - 0.005);. Commented Oct 28, 2016 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. Commented Oct 28, 2016 at 13:48

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

Great minds think alike. Almost the same answer as mine. :)
Glad to know @Justin. :D
This is good but I need the formatting for the coma. The format recognizes the machine I'm working on and it can recognize if its an engligh or french user. I guess I could use a .replace but then it needs to be a String which is why I came up with converting a string.format to double. Could I get the coma with the rounding mode ?
@dtrembl5 I have updated the answer in response to your need.
beautiful! Thanks you PseudoAj!
|
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

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.