[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

another convert double to string question

Posted on 2006-04-03
9
Medium Priority
?
446 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!
0
Comment
Question by:gagaliya
  • 5
  • 2
  • 2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16364106
System.out.println( String.format("%.2f", testdd)  );
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16364130
Or

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

Author Comment

by:gagaliya
ID: 16364191
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()?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:hoomanv
ID: 16364209
you need java 1.5 for this purpose
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16364216
Lowercase 'f'

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

but that will only work in >= 1.5

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16364228
Else

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

Expert Comment

by:hoomanv
ID: 16364253
use java.text.Class NumberFormat instead
0
 
LVL 1

Author Comment

by:gagaliya
ID: 16364280
We wont be moving to 1.5 for a while. Your last solution worked flawlessly, thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16364297
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question