Velocity Reviews > Java > Convert double to String - with only 3 decimal places

# Convert double to String - with only 3 decimal places

boris.mueller@gmail.com
Guest
Posts: n/a

 07-20-2005
Is there an easy way to convert a double to a String and specify the
number of decimal places? Let's say I only need 3 decimal places, so
the double 1.2345678 should be 1.234 as a String or 999.123456 should
be 999.123.

In Python I can do it very elegantly like this:
shortString = "%.3f" % myDouble

I am currently using the following code, but I think it is quite
dodgy... (And it ignores the scientific notation.)

public String strPre(double inValue){
String shortString;
if (inValue < 0.001){
shortString ="0";
}
else{
String doubleString = Double.toString(inValue);
String [] stringArray = doubleString.split("\\.");
String decimals = stringArray[1].substring(0,3);
shortString = stringArray[0] + "." + decimals;
}
return shortString;
}

Any help would be appreciated!

Andrew Thompson
Guest
Posts: n/a

 07-20-2005
On 20 Jul 2005 12:28:22 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Let's say I only need 3 decimal places..

OK, "you only need 3 decimal places". Do it at output..
<http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
<http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>

HTH

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info

Eric Sosman
Guest
Posts: n/a

 07-20-2005

Andrew Thompson wrote:
> On 20 Jul 2005 12:28:22 -0700, (E-Mail Removed) wrote:
>
>
>>Let's say I only need 3 decimal places..

>
>
> OK, "you only need 3 decimal places". Do it at output..
> <http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
> <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>

NumberFormat will do what boris.mueller probably
wants, but I don't think it will do what he asked for.

--
(E-Mail Removed)

Andrew Thompson
Guest
Posts: n/a

 07-20-2005
On Wed, 20 Jul 2005 16:07:16 -0400, Eric Sosman wrote:

> Andrew Thompson wrote:
>> On 20 Jul 2005 12:28:22 -0700, (E-Mail Removed) wrote:
>>
>>>Let's say I only need 3 decimal places..

>>
>> OK, "you only need 3 decimal places". Do it at output..
>> <http://java.sun.com/j2se/1.5.0/docs/api/java/text/NumberFormat.html>
>> <http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html>

>
> NumberFormat will do what boris.mueller probably
> wants, but I don't think it will do what he asked for.

"Don't give them what they ask for, give them what they need"
...Can you tell why I don't work in a service industry?

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info

boris.mueller@gmail.com
Guest
Posts: n/a

 07-20-2005
OK, I followed Andrews hints and got this far:

import java.text.DecimalFormat;
public String strPre(double inValue){
DecimalFormat threeDec = new DecimalFormat("0.000");
String shortString = (threeDec.format(inValue));
return shortString;
}

This basically works, but I am getting a comma as a seperator - not a
dot. I assume that's because I am working on a german system... Any
ideas?

boris.mueller@gmail.com
Guest
Posts: n/a

 07-20-2005
I have added the Locale and I am now getting the desired result. Still
seems to be a bit of a hack...

public String strPre(double inValue){
String shortString = "";
DecimalFormat threeDec = new DecimalFormat("0.000", new
DecimalFormatSymbols(Locale.US));
shortString = (threeDec.format(inValue));
return shortString;
}

Anyway, thanks for the help.

Paul Bilnoski
Guest
Posts: n/a

 07-20-2005
(E-Mail Removed) wrote:
> Is there an easy way to convert a double to a String and specify the
> number of decimal places? Let's say I only need 3 decimal places, so
> the double 1.2345678 should be 1.234 as a String or 999.123456 should
> be 999.123.
>
> In Python I can do it very elegantly like this:
> shortString = "%.3f" % myDouble
>
> I am currently using the following code, but I think it is quite
> dodgy... (And it ignores the scientific notation.)
>
> public String strPre(double inValue){
> String shortString;
> if (inValue < 0.001){
> shortString ="0";
> }
> else{
> String doubleString = Double.toString(inValue);
> String [] stringArray = doubleString.split("\\.");
> String decimals = stringArray[1].substring(0,3);
> shortString = stringArray[0] + "." + decimals;
> }
> return shortString;
> }
>
> Any help would be appreciated!
>

What's wrong with this?
public String strPre(double inValue) {
return Double.toString(((int)(inValue * 1000))/1000.0);
}

You would have to pad trailing zeros if you always needed three, but it
gives you the precision you want in the String.

You could also use this, or a modified version, to ensure padding and
use formatting codes (if you're more comfortable with them and can use
Java 5 features):
public String strPre(double inValue) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.printf("%.3f", inValue);
return sw.toString();
}
--Paul

john
Guest
Posts: n/a

 07-21-2005
Wed, 20 Jul 2005 13:54:23 -0700, boris.mueller wrote:

> I have added the Locale and I am now getting the desired result. Still
> seems to be a bit of a hack...
>
> public String strPre(double inValue){
> String shortString = "";
> DecimalFormat threeDec = new DecimalFormat("0.000", new
> DecimalFormatSymbols(Locale.US));
> shortString = (threeDec.format(inValue));
> return shortString;
> }
>
> Anyway, thanks for the help.

Try the following. A little cleaner without the need for the locale, and
will eliminate the groupings.

import java.text.DecimalFormat;

public String strPre(double inValue){
DecimalFormat threeDec = new DecimalFormat("0.000");
threeDec.setGroupingUsed(false);
return threeDec.format(inValue);
}

Regards,
John

Alan Krueger
Guest
Posts: n/a

 07-21-2005
Paul Bilnoski wrote:
> What's wrong with this?
> public String strPre(double inValue) {
> return Double.toString(((int)(inValue * 1000))/1000.0);
> }

For whatever reason, for all values I've cared to test in the range

0.001 <= i < 0.01

this function returns "0.00X0", where X is the digit that remains after
the transform. (JDK 1.5.0_03 on Windows XP)

boris
Guest
Posts: n/a

 07-21-2005
> DecimalFormat threeDec = new DecimalFormat("0.000");

If I leave out the locale, I am stuck again with the comma. (0,123