# How to convert 0.0 to 0.00

Hi experts,

I tried to convert 0.0 to 0.000 through the following fn.

public double getThreeRoundVal(double val)  {

DecimalFormat df = new DecimalFormat("#######0.000");
double output =  Double.valueOf(df.format(val)).doubleValue();
return output;
}
here i am getting "output" as  0.0 only. If i print the "df.format(val)" i will get 0.000. But converting it again into double truncating the remaining two zeros. And my requirement is that this function should return a double value 0.000.
So ppl plz help me in this issue....
LVL 2
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
df.setMinDecimalDigits(3);
0
Commented:
Sorry

etMinimumFractionDigits
0
Author Commented:
SORRY THE  TITLE OF THE QUESTION IS  "HOW TO CONVERT 0.0 TO 0.000".
0
Commented:
But looking closer, i see you are returning a double. Numbers don't have leading or trailing zeros - only strings do
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
public String getThreeRoundVal(double val)
{
DecimalFormat df = new DecimalFormat("#######0.000");
return df.format(val) ;
}

As CEHJ has said, it needs to be returned as a string, otherwise trailing zeros are ignored.
0
Author Commented:
hai CEHJ,

public double getThreeRoundVal(double val)  {
DecimalFormat df = new DecimalFormat("#######0.000");
df.setMinimumFractionDigits(3);
String inter =df.format(val);
double output =  Double.valueOf(inter).doubleValue();
return output;
}
here i am getting "inter" as 0.000 . but converting it again to double value truncating the remaining two zeros. I am getting "output" as 0.0 only..Please tell me how can i convert a String value to double without loosing the precisions.
0
Commented:
Hi jjohnpaul,

As explained earlier, double is a numeric datatype and hence cannot hold the leading and trailing zeros.
You will have to return a String from your function for having the value as 0.000

Cheers!
0
Commented:
>>Please tell me how can i convert a String value to double without loosing the precisions.

See my last comment
0
Commented:
jjohnpaul, you completely misunderstand double.
The zero's (as already mentioned) come when you want to print a number to the outworld.

Note this: loss of precision is ONLY caused by useless conversions to and
from Strings (in ALL programming languages in the world!).

;JOOP!
0
Commented:
> And my requirement is that this function should return a double value 0.000.

Not possible sorry, it can return a value that approximates 0.000 but you cannoyt control the numberr of decimal paces that it will display as without converting it to a string as InteractiveMind posted above

following covers details about floating point numbers

http://mindprod.com/jgloss/floatingpoint.html
0
Commented:

I think you want to use it as display perpose. So return String and display, And want to use double for any operation then that does mean to return 000 after decimal point

This thing is not possible when you convert it into Double/Float it will automatically remove all extra "0" after decimal point
if there is some value after few "0" it will show all "0"
0
Commented:
First of all, the "0.0" representation doesn't mean that the double only has a 2 digit precision. All doubles have a 52 bit precision, which is a little over 15 decimal digits.
If you put a double where a string is expected, java automatically converts it to a string for you (removing trailing zeros).
The only way around it would be to write your own function to write your own double to string function.
Example:
private final String Round3(double value)
{
DecimalFormat df = new DecimalFormat("#######0.000");
return df.format(val);
}

This function would have to be used in every print statement (everywhere a double needs to be converted into a string).
Example:
System.out.println("The number is "+Round3(mydouble));

Your function getThreeRoundVal, converts a double into a string, using the formatting you specified.
After that, you convert the string back into a double.
The only effect this function has, is dropping every digit after the third fraction digit.
This makes you loose a lot of precision, the opposite of what you seem to be after.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.