Possible Duplicate:
round double to two decimal places in java
I want to round up the double value upto 2 decimal points.
for example: I have double d=2; and the result should be result =2.00
Using Math.round() accepts a double value and converts it into the nearest long value by adding 0.5 to the value and truncating its decimal points. The long value can then be converted to an int using typecasting.
Shift the decimal of the given value to the given decimal point by multiplying 10^n. Take the floor of the number and divide the number by 10^n. The final value is the truncated value.
Math.round(number*100.0)/100.0;
double RoundTo2Decimals(double val) { DecimalFormat df2 = new DecimalFormat("###.##"); return Double.valueOf(df2.format(val)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With