# Convert double to String - with only 3 decimal places

Discussion in 'Java' started by boris.mueller@gmail.com, Jul 20, 2005.

1. ### Guest

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!

, Jul 20, 2005

2. ### Andrew ThompsonGuest

On 20 Jul 2005 12:28:22 -0700, 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

Andrew Thompson, Jul 20, 2005

3. ### Eric SosmanGuest

Andrew Thompson wrote:
> On 20 Jul 2005 12:28:22 -0700, 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.

--

Eric Sosman, Jul 20, 2005
4. ### Andrew ThompsonGuest

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

> Andrew Thompson wrote:
>> On 20 Jul 2005 12:28:22 -0700, 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

Andrew Thompson, Jul 20, 2005
5. ### Guest

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?

, Jul 20, 2005
6. ### Guest

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.

, Jul 20, 2005
7. ### Paul BilnoskiGuest

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

Paul Bilnoski, Jul 20, 2005
8. ### johnGuest

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

john, Jul 21, 2005
9. ### Alan KruegerGuest

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)

Alan Krueger, Jul 21, 2005
10. ### borisGuest

> DecimalFormat threeDec = new DecimalFormat("0.000");

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

boris, Jul 21, 2005
11. ### Alan KruegerGuest

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

Groupings are the non-significant separators at thousands, millions,
etc. places. This is not to what he was referring. In many (most?)
European locales, the decimal separator is a comma not a dot, and the
grouping separator is a dot not a comma.

He's seeing a different decimal separator than what he wants, and needs
the specific locale to get what he wants.

Alan Krueger, Jul 21, 2005
12. ### Alan KruegerGuest

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;
> }

You could shorten it a little by eliminating the shortString variable
and returning the result of threeDec.format(inValue) directly.

Alan Krueger, Jul 21, 2005
13. ### WayneGuest

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!
>

I don't know why no one pointed this out yet, but Java5 has added
printf fuctions. You can do what you with much like with
Python (or C or Perl or ...):

String s = String.format( "%.3f", myDouble );

or:

String s = String.format( myLocale, "%.3f", myDouble );

-Wayne

Wayne, Jul 21, 2005
14. ### Andrew ThompsonGuest

On Thu, 21 Jul 2005 16:41:43 GMT, Wayne wrote:

> I don't know why no one pointed this out yet,

Paul Bilnoski..

>..but Java5 has added printf fuctions.

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Featuring Gratuitous Alien Nudity

Andrew Thompson, Jul 21, 2005

Joined:
Jul 6, 2012
Messages:
1