Convert double to String - with only 3 decimal places

B

boris.mueller

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

Andrew Thompson

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? ;-)
 
B

boris.mueller

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?
 
B

boris.mueller

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.
 
P

Paul Bilnoski

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
 
J

john

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
 
A

Alan Krueger

Paul said:
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)
 
B

boris

DecimalFormat threeDec = new DecimalFormat("0.000");

If I leave out the locale, I am stuck again with the comma. (0,123
instead of 0.123.)
 
A

Alan Krueger

john said:
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.
 
A

Alan Krueger

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.
 
W

Wayne

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
 
Joined
Jul 6, 2012
Messages
1
Reaction score
0
This response is for everyone else that gets to this site years later:
String.format("%.3f", <double>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top