Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert double to 2 number after the dot? [duplicate]

Tags:

java

android

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

like image 989
goldsoft Avatar asked Sep 14 '11 11:09

goldsoft


People also ask

How do you show 2 digits after a decimal?

format(“%. 2f”) We also can use String formater %2f to round the double to 2 decimal places.

How do you convert a double to two decimal places?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

What is the 2 decimal place?

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.


2 Answers

try this code

double x=123.45678;
DecimalFormat df = new DecimalFormat("#.##");
String dx=df.format(x);
x=Double.valueOf(dx);
like image 28
Balaji.K Avatar answered Oct 12 '22 23:10

Balaji.K


x = Math.floor(x * 100) / 100;
like image 88
Oliver Charlesworth Avatar answered Oct 12 '22 23:10

Oliver Charlesworth