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