Double value to round up in Java

java round double to 2 decimal places
java round up double to two decimal places
math.round java
java round up or down
double to int java
java truncate double to 2 decimal places
how to set precision for float value in java
java double parsedouble rounding

I have a double value = 1.068879335 i want to round it up with only two decimal values like 1.07.

I tried like this

DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = Double.parseDouble(formate) ;

this is giving me this following exception

java.lang.NumberFormatException: For input string: "1,07"
     at sun.misc.FloatingDecimal.readJavaFormatString(
     at java.lang.Double.parseDouble(

can some one tell me what is wrong with my code.

finaly i need the finalValue = 1.07;

Note the comma in your string: "1,07". DecimalFormat uses a locale-specific separator string, while Double.parseDouble() does not. As you happen to live in a country where the decimal separator is ",", you can't parse your number back.

However, you can use the same DecimalFormat to parse it back:

DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value); 
double finalValue = (Double)df.parse(formate) ;

But you really should do this instead:

double finalValue = Math.round( value * 100.0 ) / 100.0;

Note: As has been pointed out, you should only use floating point if you don't need a precise control over accuracy. (Financial calculations being the main example of when not to use them.)

Double value to round up in Java, double value = 1.125879D; double valueRounded = Math.round(value * 100D) / 100D;. Second, when you print or convert real number to string� In order to round float and double numbers in Java, we use the java.lang.Math.round () method. The method accepts either double or float values and returns an integer value. It returns the closest integer to number. This is computed by adding ½ to the number and then flooring it.

Live @Sergey's solution but with integer division.

double value = 23.8764367843;
double rounded = (double) Math.round(value * 100) / 100;
System.out.println(value +" rounded is "+ rounded);


23.8764367843 rounded is 23.88

EDIT: As Sergey points out, there should be no difference between multipling double*int and double*double and dividing double/int and double/double. I can't find an example where the result is different. However on x86/x64 and other systems there is a specific machine code instruction for mixed double,int values which I believe the JVM uses.

for (int j = 0; j < 11; j++) {
    long start = System.nanoTime();
    for (double i = 1; i < 1e6; i *= 1.0000001) {
        double rounded = (double) Math.round(i * 100) / 100;
    long time = System.nanoTime() - start;
    System.out.printf("double,int operations %,d%n", time);
for (int j = 0; j < 11; j++) {
    long start = System.nanoTime();
    for (double i = 1; i < 1e6; i *= 1.0000001) {
        double rounded = (double) Math.round(i * 100.0) / 100.0;
    long time = System.nanoTime() - start;
    System.out.printf("double,double operations %,d%n", time);


double,int operations 613,552,212
double,int operations 661,823,569
double,int operations 659,398,960
double,int operations 659,343,506
double,int operations 653,851,816
double,int operations 645,317,212
double,int operations 647,765,219
double,int operations 655,101,137
double,int operations 657,407,715
double,int operations 654,858,858
double,int operations 648,702,279
double,double operations 1,178,561,102
double,double operations 1,187,694,386
double,double operations 1,184,338,024
double,double operations 1,178,556,353
double,double operations 1,176,622,937
double,double operations 1,169,324,313
double,double operations 1,173,162,162
double,double operations 1,169,027,348
double,double operations 1,175,080,353
double,double operations 1,182,830,988
double,double operations 1,185,028,544

Java Double Round off to 2 decimal always, Java - How to round double / float value to 2 decimal points. UP); System.out. println("salary : " + df.format(input)); //1205.64 } }. Copy. Output. Double value to round up in Java. Ask Question Asked 8 years, 11 months ago. Active 9 months ago. Viewed 173k times 37. 24. I have a double value = 1. 068879335 i

The problem is that you use a localizing formatter that generates locale-specific decimal point, which is "," in your case. But Double.parseDouble() expects non-localized double literal. You could solve your problem by using a locale-specific parsing method or by changing locale of your formatter to something that uses "." as the decimal point. Or even better, avoid unnecessary formatting by using something like this:

double rounded = (double) Math.round(value * 100.0) / 100.0;

Java, 1 Answer. double roundOff = Math.round(a * 100.0) / 100.0; Output is. 123.14. Or. double roundOff = (double) Math. round(a * 100) / 100; this will do it for you as well. The java.lang.Math.round(double a) returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. Special cases − If the argument is NaN, the result is 0.

There is something fundamentally wrong with what you're trying to do. Binary floating-points values do not have decimal places. You cannot meaningfully round one to a given number of decimal places, because most "round" decimal values simply cannot be represented as a binary fraction. Which is why one should never use float or double to represent money.

So if you want decimal places in your result, that result must either be a String (which you already got with the DecimalFormat), or a BigDecimal (which has a setScale() method that does exactly what you want). Otherwise, the result cannot be what you want it to be.

Read The Floating-Point Guide for more information.

How to round up to 2 decimal places in java?, In order to round float and double numbers in Java, we use the java.lang. The method accepts either double or float values and returns an in . round-off of "+ x + " is "+ Math.round(x)); System.out.println("The round-off of "+� In Java, there are a few ways to round float or double to 2 decimal places.

Try this: org.apache.commons.math3.util.Precision.round(double x, int scale)


Apache Commons Mathematics Library homepage is:

The internal implemetation of this method is:

public static double round(double x, int scale) {
    return round(x, scale, BigDecimal.ROUND_HALF_UP);

public static double round(double x, int scale, int roundingMethod) {
    try {
        return (new BigDecimal
               .setScale(scale, roundingMethod))
    } catch (NumberFormatException ex) {
        if (Double.isInfinite(x)) {
            return x;
        } else {
            return Double.NaN;

Round float and double numbers in Java, java. lang. Math. round(double a) � If the argument is NaN, the result is 0. � If the argument is negative infinity or any value less than or equal to the value of Long. double*100.0 – 234354.76 Math.round(double*100.0) – 234355.00 (round to nearest value) Math.round(double*100.0)/100.0 – 2343.55

Java.lang.Math.round() Method, System.out.printf("Value with 3 digits after decimal point %.3f %n", PI);. // OUTPUTS: private static double round(double value, int places) {. Java Math.round() method. The java.lang.Math.round() is used round of the decimal numbers to the nearest value. This method is used to return the closest long to the argument, with ties rounding to positive infinity.

How to Round a Number to N Decimal Places in Java, Java math FAQ: How do I round a float or double number to an integer static void main(String[] args) { System.out.println(Math.round(1.9)); // 2� double value = 200.3456; System.out.printf("Value: %.2f", value); If you want to have the result in a String instead of being printed to the console, use String.format () with the same arguments: String result = String.format("%.2f", value); Or use class DecimalFormat:

Java: How to round a float or double to an integer, Out of these 3 ways there is one way in which you can round of the double value to nearest integer value. 1. Convert double to int using typecasting 2. Convert� But if round thinks it’s receiving a double it will return a long, so casting it to an int like this is necessary: int y = (int) Math.round (2.6); The first of these two examples works because I have explicitly given round a float when I wrote 2.6f. In summary, if you want to round a float or double to an int in Java, I hope this is helpful.

  • if you round this up, 1.085879335, 2 digit precision it will be 1.09 and 1.08 if round down. how come you want to get 1.07 out of 1.085879335?
  • +1 for actually identifying the problem. You can also explicitly tell DecimalFormat to use the C locale.
  • As I pointed out in my answer, this algorithm does not generally work. And for as long as Java sticks with IEEE floating point such an algorithm does not exist.
  • @Waldheinz Given the context of the question, my answer is correct. Fair play for you thinking one step further, but should I know downvote your answer because you completely failed to identify why the code failed in the first place? Of course I shouldn't.
  • Sorry, I did not want to offend. Just wanted to point out that even the corrected code does not work for every input. But then, you answered the original question.
  • @Waldheinz It's fine, I just thought it was unnecessary to edit my answer with this important gotcha because you explained it really well. Next time I won't be so lazy. :)
  • Where is the integer division? The cast operator has higher precedence, so you divide a double by an integer, but integer is promoted to double in such case. So it is essentially the same as my code. Or the same as Math.round(value * 100) / 100.0. It's just a matter of style. If you used genuine integer division, you'd get 23 as the result.
  • @Sergey, In theory, you are right and perhaps my answer is JVM specific, see my edit for more details.
  • awesome. The JLS clearly states that integer must be promoted to double in such division, so the result must be the same for both cases in any conforming Java implementation, but I had no idea that JVM optimizes actual implementation in such way.
  • you should ALWAYS explicitly set the native datatype next to your static numbers in your source code, and don't leave it up to the compiler to decide for you. do you? I hope you do not. this is important when writing high performance games.
  • How about this coming up with 23.8764367843 => 23.88000000002? I think a String processing is better.
  • @Manidip, well, there is no way you get exactly 23.88 by parsing "23.88" as double either, as 23.88 isn't exactly representable in binary anyway. If exact representation is needed, some sort of arbitrary precision library should be used.
  • You are right, but I thought we were talking about "textual" and "approximate" representation, not binary. Waldheinz and Peter answered that above. DecimalFormat is a good choice, but in case it doesn't work, I'd try a String roundedStr = String.valueOf (rounded); int dotPoint = roundedStr.indexOf("."); return (roundedStr).substring(0,dotPoint < 0 ? roundedStr.length() : dotPoint+2); This is not totally correct, you might have to add padding 0's to the right if there is only 1 digit to the right of the decimal point in roundedStr - but you get the idea what I was commenting about earlier.