How can I print a double value 0.0 to 0.00?

Posted on 2006-03-29
Last Modified: 2012-05-05

I am trying to print a string value to Double .But i need to print in different format..

I have used the following code

String s1 = "2"
String s2 = ".1"
Double d1 = new Double(s1);
Double d2 = new Double(s2);
System.out.println("First double"+d1+"### Second double "+d2);

Its pirnting
 FirstDouble 2 ### Second double 0.1.

But i want it be printed as
d1 as 2.00 and  d2 as 0.10

How can it be possible?


Question by:vijayam
    LVL 92

    Expert Comment

    NumberFormat nf = new DecimalFormat("0.00");
    s1 = nf.format(d1.doubleValue());
    LVL 92

    Expert Comment

    LVL 86

    Accepted Solution

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

    Author Comment

    I am getting the output,but it is as string but i need it as Double.

    DecimalFormat df  = new DecimalFormat("0.00");
    FieldPosition fp = new FieldPosition (NumberFormat.INTEGER_FIELD);
    StringBuffer sb= new StringBuffer();
    sb = df.format( d1 , sb, fp );

    then its printing sb as 2.00

    But i need it to be as Double  that's why  i coverted this to string and

    Double dd = new Double(sb.toString());

    then its printing,same 2 again...what should i do,if i want to print 2.00 as Double object.

    LVL 86

    Expert Comment

    The representation of an object, when materialized as a String, has a varying relation to its binary content. In the case of numbers, or object wrappers of numbers, the binary form is, and should be, distinct and different
    LVL 92

    Assisted Solution

    doubles are floating point values, you cannot format them directly.
    you format them in a string as required

    System.out.println("First double"+nf.format(d1.doubleValue())+"### Second double "+nf.format(d2.doubleValue()));
    LVL 30

    Assisted Solution

    A double/ Double holds the *value*, not the *format*.
    LVL 37

    Assisted Solution

    >> A double/ Double holds the *value*, not the *format*.

    >> But i need it to be as Double
    That's the value
    >> then its printing,same 2 again
    That's the format

    Your request is like saying I have this

          int i = 2;

    and I want it to be 0002 instead of 2.

    2 is the value, 0002 is the format, just a representation.
    Your Double is 2.0, how you represent it is your choice.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now