[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# How to convert 0.0 to 0.00

Posted on 2006-04-21
Medium Priority
2,914 Views
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....
0
Question by:jjohnpaul

LVL 86

Expert Comment

ID: 16505365
df.setMinDecimalDigits(3);
0

LVL 86

Expert Comment

ID: 16505379
Sorry

etMinimumFractionDigits
0

LVL 2

Author Comment

ID: 16505385
SORRY THE  TITLE OF THE QUESTION IS  "HOW TO CONVERT 0.0 TO 0.000".
0

LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 16505388
But looking closer, i see you are returning a double. Numbers don't have leading or trailing zeros - only strings do
0

LVL 25

Expert Comment

ID: 16505396
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

LVL 2

Author Comment

ID: 16505424
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

LVL 10

Assisted Solution

Sys_Prog earned 500 total points
ID: 16505430
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

LVL 86

Expert Comment

ID: 16505432
>>Please tell me how can i convert a String value to double without loosing the precisions.

See my last comment
0

LVL 24

Assisted Solution

sciuriware earned 500 total points
ID: 16505550
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

LVL 92

Assisted Solution

objects earned 500 total points
ID: 16505596
> 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

Expert Comment

ID: 16507241

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

Expert Comment

ID: 16587098
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month11 days, 2 hours left to enroll