Computing.Net > Forums > Programming > Java double question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java double question

Reply to Message Icon

Name: hymn7
Date: March 25, 2005 at 04:22:22 Pacific
OS: XP
CPU/Ram: 512 mb
Comment:

In Java, how do you make it where if you have a double with a lot of spaces (like 5.120230061 for example) print out with only two spaces? (like 5.12)



Sponsored Link
Ads by Google

Response Number 1
Name: Dr. Nick
Date: March 25, 2005 at 13:07:08 Pacific
Reply:

You use the Format method of the String class.

Your example would be like this:

public class Test
{
    public static void main(String[] args)
    {
        double d = 3.14159;
        String s;

        s = String.format("%.2f", d);
        System.out.println(s);
    }
}

That will print "3.14".


0

Response Number 2
Name: hymn7
Date: March 27, 2005 at 06:54:51 Pacific
Reply:

Ah.. I see.. I've been trying to figure that out for a while now. Thank you!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java double question

Java Method Question www.computing.net/answers/programming/java-method-question/14684.html

Java noob question www.computing.net/answers/programming/java-noob-question/13768.html

java BufferedReader question www.computing.net/answers/programming/java-bufferedreader-question/10020.html