Java – How to round double / float value to 2 decimal places
There are a few ways to round float
or double
to 2 decimal places in Java.
Table of contents
- 1. DecimalFormat("0.00")
- 2. DecimalFormat("0.00") vs DecimalFormat("#.##")
- 3. BigDecimal
- 4. String.format("%.2f", input)
- 5. Math.round
- 6. References
Note
In short, for monetary calculation, picks BigDecimal
; for display purpose, picks DecimalFormat("0.00")
.
1. DecimalFormat(“0.00”)
We can use DecimalFormat("0.00")
to ensure the number always round to 2 decimal places. For DecimalFormat
, the default rounding mode is RoundingMode.HALF_EVEN
, and we can use setRoundingMode(RoundingMode)
to set a specified rounding mode.
package com.mkyong.math.rounding;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalExample {
private static final DecimalFormat df = new DecimalFormat("0.00");
public static void main(String[] args) {
double input = 1205.6358;
System.out.println("salary : " + input);
// DecimalFormat, default is RoundingMode.HALF_EVEN
System.out.println("salary : " + df.format(input)); //1205.64
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("salary : " + df.format(input)); //1205.63
df.setRoundingMode(RoundingMode.UP);
System.out.println("salary : " + df.format(input)); //1205.64
}
}
Output
salary : 1205.6358
salary : 1205.64
salary : 1205.63
salary : 1205.64
2. DecimalFormat(“0.00”) vs DecimalFormat(“#.##”)
The below shows the difference between DecimalFormat("0.00")
and DecimalFormat("#.##")
.
package com.mkyong.math.rounding;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalExample2 {
private static final DecimalFormat dfZero = new DecimalFormat("0.00");
private static final DecimalFormat dfSharp = new DecimalFormat("#.##");
public static void main(String[] args) {
double input = 1205.6;
System.out.println("salary : " + input);
System.out.println("salary 0.00 : " + dfZero.format(input));
System.out.println("salary #.## : " + dfSharp.format(input));
double input2 = 1205.60;
System.out.println("salary : " + input2);
System.out.println("salary 0.00 : " + dfZero.format(input2));
System.out.println("salary #.## : " + dfSharp.format(input2));
}
}
Output
salary : 1205.6
salary 0.00 : 1205.60
salary #.## : 1205.6
salary : 1205.6
salary 0.00 : 1205.60
salary #.## : 1205.6
DecimalFormat("#.##")
displays blank if the second decimal place is empty or zero. The DecimalFormat("0.00")
is a better solution for 2 decimal places.
3. BigDecimal
We also can convert the double
to a BigDecimal
object and set the scale and rounding mode.
package com.mkyong.math.rounding;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalExample {
public static void main(String[] args) {
//double input = 3.14159265359;
double input = 1205.6358;
System.out.println("double : " + input);
// convert double to BigDecimal
BigDecimal salary = new BigDecimal(input);
System.out.println("BigDecimal: " + salary);
// round to 2 decimal places
BigDecimal salary2 = salary.setScale(2, RoundingMode.HALF_UP);
System.out.println("BigDecimal: " + salary2);
// one line
BigDecimal salary3 = new BigDecimal(input).setScale(2, RoundingMode.HALF_UP);
System.out.println("BigDecimal: " + salary3);
// convert BigDecimal back to double
double salary4 = salary3.doubleValue();
System.out.println("double : " + salary4);
}
}
Output
double : 1205.6358
BigDecimal: 1205.63580000000001746229827404022216796875
BigDecimal: 1205.64
BigDecimal: 1205.64
double : 1205.64
4. String.format(“%.2f”, input)
The String.format is working fine, and the default rounding is half-up; however, we have no way to configure the type of rounding mode.
package com.mkyong.math.rounding;
public class StringFormatExample {
public static void main(String[] args) {
double input = 1205.6358;
System.out.println("salary : " + input);
// round half-up, no way control
// 1205.64
System.out.println("salary : " + String.format("%.2f", input));
// 1205.64
System.out.format("salary : %.2f", input);
}
}
Output
salary : 1205.6358
salary : 1205.64
salary : 1205.64
5. Math.round
5.1 This Math.round
is for educational purposes 🙂
package com.mkyong.math.rounding;
public class MathExample {
public static void main(String[] args) {
double input = 1205.6358;
System.out.println("salary : " + input);
double salary = Math.round(input * 100.0) / 100.0;
System.out.println("salary : " + salary);
}
}
Output
salary : 1205.6358
salary : 1205.64
input = 1205.6358;
Math.round(input * 100.0) / 100.0;
Math.round(120563.58) / 100.0;
120564 / 100.0;
salary = 1205.64
5.2 For 3 decimal places, try * 1000
.
double input = 1205.6358;
double salary = Math.round(input * 1000.0) / 1000.0;
System.out.println("salary : " + salary);
Output
salary : 1205.6358
salary : 1205.636
I created an account just to say thank you.
(12465,2) than it gives 12500 and (12465,3) than it gives 12000 can any one have idea to write such method.in java
this question was asked to me at interview.
Hi Senior programmer, I am glad you shared your ideas online for us to learn. Well done sir.
First: Math.round(123.50 * 100.0) = 124.0, ok.
Second: Math.round(-123.50 * 100.0) = -123.0!! is this correct?, I wait -124.0.
Isn’t nearest number -124.0 to -123.50?
I had the same issue, Math.round method is overloaded and you are getting the long back make sure you pick the method that returns an int.
please can we do this format like 1,200.00 is yes any ideas to share, please
THANKS HELPED OUT!!
DecimalFormat df = new DecimalFormat(“0.00”);
double time = 1205.00;
time = Double.valueOf(df.format(time));
System.out.println(time);
// output 1205.0 but expect 1205.00
is DecimalFormat class name? Please reply asap
Need DecimalFormat for display in 2 decimal points.
System.out.println(ft.format(time)); // output 1205.00
good good
Thanks for this. Its useful. Saved sometime. You are hero.
In DecimalFormat(“0.00”) or DecimalFormat(“#.##”) ??
In DecimalFormat we should use 0 or # ??
For 1205.61
#.## display 1205.61
0.00 display 1205.61
For 1205.60
#.## display 1205.6
0.00 display 1205.60
For 1205.6
#.## display 1205.6
0.00 display 1205.60
Thank you much…DecimalFormat is really good solution.
It is better than Math.Round!
int i = 180;
int j = 1;
double div= ((double)(j*100)/i);
DecimalFormat df = new DecimalFormat(“#.00”); // simple way to format till any deciaml points
System.out.println(div);
System.out.println(df.format(div));
EditText GridBase = (EditText) findViewById(R.id.GridBase);
Double GridBasedbl = Double.parseDouble(GridBase.getText().toString());
//
TextView VoltageBase = (TextView) findViewById(R.id.VoltageBase);
Double VoltageBasedbl = Double.parseDouble(VoltageBase.getText().toString());
//
Double product = GridBasedbl*VoltageBasedbl;
Log.d(“Calc product”, Double.toString(product));
EditText GridVoltage = (EditText) findViewById(R.id.GridVoltage);
GridVoltage.setText(String.format(“%.3f”, product));
Log.d(“Calc GridVoltage”, GridVoltage.getText().toString());
i want to convert -0.00000000758602002145 to zero in two decimal accuracy.
double kilobytes = -0.00000000758602002145;
DecimalFormat df = new DecimalFormat(“###.##”);
System.out.println(“kilobytes (DecimalFormat) : ” + df.format(kilobytes));
the result i got is “-0”
What to do … ?
Please refer that
double lvForeignPremiumAmount=6208.125
System.out.println(“AFT Math.round(lvForeignPremiumAmount*100.0)/100.0 =”+Math.round(lvForeignPremiumAmount*100.0)/100.0);
System.out.println(“AFT formatter.format(lvForeignPremiumAmount)=”+formatter.format(lvForeignPremiumAmount));
Therefor Please advice which method could be more accurate.
I want to convert value like 5.8987221219522616E-5…want to avoid E value..please let me know if have any idea about it…thanks
man, thank you so much.
Saved me a lot of time. Most didatic explanation i’ve found.
Keep up with the good work, bye.
thanks for that tip
very helpful
millions of Thanks