This discussion is archived
13 Replies Latest reply: Apr 13, 2011 8:30 AM by 796440

# Regarding Double Conversion & Decimal Precision

Currently Being Moderated
Hi,

Here I'm converting a number into double & trying to use the precision upto 2 places
private String convertToDouble(String fieldValue)
{
double value = Double.parseDouble(fieldValue.replace(',', '.'));
value = (double) Math.round(value * 100) / 100;
return Double.toString(value);

}
It's ok for short numbers e.g. 5555
But When I enter numbers like 9999999999 the output is
9.999999999E9

All I want is number should be converted in double then it should be precised upto 2 decimal places.

Edited by: Mahi on Apr 14, 2011 3:43 AM
• ###### 1. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Use the NumberFormat class for such purposes, not Double.toString().
• ###### 2. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Hello,
{noformat} tags when posting code, so that the code appears formatted:

private String convertToDouble(String fieldValue)
{
double value = Double.parseDouble(fieldValue.replace(',', '.'));
value = (double) Math.round(value * 100) / 100;
return Double.toString(value);

}
It's ok for short numbers e.g. 5555
But When I enter numbers like 9999999999 the output is
9.999999999E9
If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called "computerized scientific notation." (...)
• ###### 3. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Thanks for the response...But the actual problem is to show the the 2 numbers after decimal.
Doesn't matter it's in exponential form or not.
I'm passing string there so to convert it into decimal I'm trying to parse it first & obviously that is needed.

Please anymore solution to parse the string so that I can restrict the precision upto 2 decimal places.
It would be really greatful
• ###### 4. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Mahi wrote:
Thanks for the response...But the actual problem is to show the the 2 numbers after decimal.
Doesn't matter it's in exponential form or not.
Yes it does. An exponential form with only two DPs will render a totally different value to a decimal number to 2DPs.
I'm passing string there so to convert it into decimal I'm trying to parse it first & obviously that is needed.
If you use the Java BigDecimal class to store the resulting number, then it will accept strings in standard form. It also allows you to specify the scale when it is [url http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal%28java.lang.String,%20java.math.MathContext%29]constructed, which saves a lot of faffing about with number formats.

However, if you're absolutely determined to get a non-scientific string to two decimal places, I have to admit to a preference for printf() and String.format() (old C dinosaur), and the spec for that is:
String.format("%.2f", myDouble);
Winston
• ###### 5. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Mahi wrote:
Please anymore solution to parse the string so that I can restrict the precision upto 2 decimal places.
If you want the numerical value to be accurate only to two decimal places, you'll have to use BigDecimal. There's no way to achieve that with double. However, it is more likely that you want to display the value in that particular format, but still keep more precision in the numerical value that's stored. In this case, as already pointed out, you'll use String.format() or java.text.DecimalFormat.
• ###### 6. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
jverd wrote:
Mahi wrote:
Please anymore solution to parse the string so that I can restrict the precision upto 2 decimal places.
If you want the numerical value to be accurate only to two decimal places, you'll have to use BigDecimal.
@OP: And just to add to that, a heads-up for you: If your values are coming from doubles, you need to be aware of a few gotchas with the [url http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal%28double%29]BigDecimal(double) constructor.

Winston
• ###### 7. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
YoungWinston wrote:
jverd wrote:
Mahi wrote:
Please anymore solution to parse the string so that I can restrict the precision upto 2 decimal places.
If you want the numerical value to be accurate only to two decimal places, you'll have to use BigDecimal.
@OP: And just to add to that, a heads-up for you: If your values are coming from doubles, you need to be aware of a few gotchas with the [url http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal%28double%29]BigDecimal(double) constructor.
Right. The short version being: It won't magically produce a BigD that's any more accurate than the double you feed it. For instance, since a double cannot store the value 0.1, but instead stores something very close to that, if you do this:
double d = 0.1;
BigDecimal bd = new BigDecimal(d);
System.out.println(bd);
you might be surprised at the results. You might even think they're "incorrect," but they're not.
• ###### 8. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Hi jverd/YoungWinston,
jverd wrote:
YoungWinston wrote:
jverd wrote:
Mahi wrote:
Please anymore solution to parse the string so that I can restrict the precision upto 2 decimal places.
If you want the numerical value to be accurate only to two decimal places, you'll have to use BigDecimal.
@OP: And just to add to that, a heads-up for you: If your values are coming from doubles, you need to be aware of a few gotchas with the [url http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal%28double%29]BigDecimal(double) constructor.
Right. The short version being: It won't magically produce a BigD that's any more accurate than the double you feed it. For instance, since a double cannot store the value 0.1, but instead stores something very close to that, if you do this:
double d = 0.1;
BigDecimal bd = new BigDecimal(d);
System.out.println(bd);
you might be surprised at the results. You might even think they're "incorrect," but they're not.
Solutions are better :)
The complete clarification of question.
The value which I enters in text box is first validated for number.
& in second phase Expected Results:
private String convertToDouble(String fieldValue)
{
String value1 = fieldValue.replace(",", ".");
double value = Double.parseDouble(value1);
value = (double) Math.round(value * 100) / 100;
if(!value1.contains(".")){
return value1;
}
return Double.toString(value);

}
Is that all I wanted.
I'm sorry if my question was not clear earlier.
But anyways thanks to all :)
• ###### 9. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Some more validations I'm doing but I think no use to write in this post.
• ###### 10. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Mahi wrote:
Solutions are better :)
The complete clarification of question.
The value which I enters in text box is first validated for number.
& in second phase Expected Results:
private String convertToDouble(String fieldValue)
{
String value1 = fieldValue.replace(",", ".");
double value = Double.parseDouble(value1);
value = (double) Math.round(value * 100) / 100;
if(!value1.contains(".")){
return value1;
}
return Double.toString(value);
}
Is that all I wanted.
I suspect you'll find that it still produces scientific form for large values.
It will also throw a NumberFormatException if the parseDouble() doesn't work (just FYI; it may be what you want).
To save time, here's my suggestion:
private String validateAndConvert(String fieldValue)
{
double value;
try {
value = Double.parseDouble(fieldValue);
} catch(NumberFormatException e) {
return null;
}
return String.format("%.2f", value);
}
That should correctly validate any supplied string as being OK for a double, and will return null, rather than throwing an exception, if it isn't. If it is OK, it will return the resulting value as a numeric string to 2 DPs (no scientific form).

Also: not quite sure what your String.replace() is attempting to do. parseDouble() will do its stuff according to the current Locale, so it seems redundant.

Winston
• ###### 11. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Mahi wrote:
Solutions are better :)
If you mean that handing you a complete solution to your problem would be better than giving you hints and leaving you to do the work for yourself, then you're 100% mistaken.

Did you even try this?
double d = 0.1;
BigDecimal bd = new BigDecimal(d);
System.out.println(bd);
Do you understand why it gives the result it does?
The complete clarification of question.
value = (double) Math.round(value * 100) / 100;
This is the wrong way to do it, and does not do what you think it does. It will not work in all cases.

For instance, you might think that you could end up with the value 1.1 stored in your double value variable. You cannot. It is impossible for a double to hold that value.

It might be that the above is good enough for your purposes, but I suggest you familiarize yourself with how floating point values work before using it.

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

Edited by: jverd on Feb 24, 2011 10:00 AM
• ###### 12. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
Thanks jverd
• ###### 13. Re: Regarding Double Conversion & Decimal Precision
Currently Being Moderated
No worries.

#### Legend

• Correct Answers - 10 points