We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

another convert double to string question

gagaliya
gagaliya asked
on
Medium Priority
463 Views
Last Modified: 2012-06-22
   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!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
System.out.println( String.format("%.2f", testdd)  );
CERTIFIED EXPERT
Top Expert 2016

Commented:
Or

System.out.printf("%.2f\n", testdd);

Author

Commented:
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()?
Top Expert 2006

Commented:
you need java 1.5 for this purpose
CERTIFIED EXPERT
Top Expert 2016

Commented:
Lowercase 'f'

System.out.println( String.format("%.2f", testdd) );

but that will only work in >= 1.5

CERTIFIED EXPERT
Top Expert 2016
Commented:
Else

DecimalFormat nf = new DecimalFormat("0.00");
String f = nf.format(testdd);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006

Commented:
use java.text.Class NumberFormat instead

Author

Commented:
We wont be moving to 1.5 for a while. Your last solution worked flawlessly, thank you!
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.