srikotesh
asked on
how to display decimal value with 2 digits after decimal point
Hi Experts,
I have a decimal value.
I want to display value with 2 digits after decimal point.
Example:
1234.5678 --->show as -->1234.56
1234--->(when no digits after decimal)show as 1234.00
value data type should be double.
Suggest me how to do.
I have a decimal value.
I want to display value with 2 digits after decimal point.
Example:
1234.5678 --->show as -->1234.56
1234--->(when no digits after decimal)show as 1234.00
value data type should be double.
Suggest me how to do.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
no cehj we can show it as 1234.57 (we can round it of with 1234.57)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
double theFPValue = 12345;
String value = String.format("%.2f", theFPValue);
System.out.println("2f is "+value);
System.out.println("double is "+Double.parseDouble(value));
output:2f is 12345.00
double is 12345.0
Hi Experts,
From converting String to Double instead of 12345.00 it is displaying as 12345.0
Right. What's your point there though?
ASKER
I have to display two digits after decimal even though digits are not present
12345-12345.00 --it is coming in String Format(i dont need String value)
12345-12345.00--should come with double datatype.
12345-12345.00 --it is coming in String Format(i dont need String value)
12345-12345.00--should come with double datatype.
String s = "12345.0";
String value = String.format("%.2f", Double.parseDouble(s));
System.out.println(value);
ASKER
is there possibility to get zero at end?
double theFPValue = 12345;//actual value is double only
String value = String.format("%.2f", theFPValue);// now we converted to String
System.out.println("2f is "+value);//getting 12345.00
System.out.println("double is "+Double.parseDouble(value));//after getting string formatted value now i have to change it to double data type here one zero is missing coming as 12345.0
Final output expected should be double data type with value as -->12345.00
Final output expected should be double data type
There's some confusion here. In general, you don't want to make a floating point value less precise. Why would you want to?
In general, you want output to have a particular number of decimal places. At that point, output is just a string
:)