gagaliya
asked on
another convert double to string question
public double roundDouble(double d, int places)
{
return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10, (double) places);
}
public static void main(String[] args)
{
Double testd = new Double(1.129219589E7);
double testdd = testd.doubleValue();
test tclass = new test();
testdd = tclass.roundDouble(testdd, 2);
System.out.println( String.valueOf(testdd) );
//also tried Double.toString(testdd)
}
The problem is i want it to output "11292195.89" instead of "1.129219589E7"
i cannot change the source: new Double(1.129219589E7). This is something we get in a feed/api. So i need to convert 1.129219589E7 to 11292195.89. but HOW? thank you!
{
return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10, (double) places);
}
public static void main(String[] args)
{
Double testd = new Double(1.129219589E7);
double testdd = testd.doubleValue();
test tclass = new test();
testdd = tclass.roundDouble(testdd,
System.out.println( String.valueOf(testdd) );
//also tried Double.toString(testdd)
}
The problem is i want it to output "11292195.89" instead of "1.129219589E7"
i cannot change the source: new Double(1.129219589E7). This is something we get in a feed/api. So i need to convert 1.129219589E7 to 11292195.89. but HOW? thank you!
System.out.println( String.format("%.2f", testdd) );
Or
System.out.printf("%.2f\n" , testdd);
System.out.printf("%.2f\n"
ASKER
hey cehj, thanks for the quick response! your first reply is what i am looking for -need it as a variable (to be used elsewhere). but i got the following complilation error:
//tried lower case String.format() as well
test.java:21: cannot resolve symbol
symbol : method Format (java.lang.String,double)
location: class java.lang.String
System.out.println( String.Format("%.2f", testdd) );
looked up the javadoc for string, seems there is no function named format()?
//tried lower case String.format() as well
test.java:21: cannot resolve symbol
symbol : method Format (java.lang.String,double)
location: class java.lang.String
System.out.println( String.Format("%.2f", testdd) );
looked up the javadoc for string, seems there is no function named format()?
you need java 1.5 for this purpose
Lowercase 'f'
System.out.println( String.format("%.2f", testdd) );
but that will only work in >= 1.5
System.out.println( String.format("%.2f", testdd) );
but that will only work in >= 1.5
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use java.text.Class NumberFormat instead
ASKER
We wont be moving to 1.5 for a while. Your last solution worked flawlessly, thank you!
:-)