Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round the double value to 2 decimal points? [duplicate]

Tags:

java

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

like image 372
Rakesh Sabbani Avatar asked May 10 '11 06:05

Rakesh Sabbani


People also ask

How do you round up double value?

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.

How do you truncate a double to two decimal places?

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.


2 Answers

Math.round(number*100.0)/100.0; 
like image 114
jmj Avatar answered Sep 22 '22 11:09

jmj


double RoundTo2Decimals(double val) {             DecimalFormat df2 = new DecimalFormat("###.##");         return Double.valueOf(df2.format(val)); } 
like image 35
Razan Paul Avatar answered Sep 19 '22 11:09

Razan Paul