Possible Duplicate:
Round a double to 2 significant figures after decimal point
how to convert double to 2 number after the dot ?
for example:
double x=123.45678;
i need that x=123.45
(in java for android)
thanks in advance
format(“%. 2f”) We also can use String formater %2f to round the double to 2 decimal places.
Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.
Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.
try this code
double x=123.45678;
DecimalFormat df = new DecimalFormat("#.##");
String dx=df.format(x);
x=Double.valueOf(dx);
x = Math.floor(x * 100) / 100;
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