Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 28-03-2009, 04:35 PM
JavaPF's Avatar
mmm.. coffee
 
5 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,211
Thanks: 61
Thanked 66 Times in 64 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Inspired
Post How to Format a double value to 2 decimal places

This code allows you to round a double value to 2 decimal places using DecimalFormat.

You can increase the decimal places by adding an extra #. For example DecimalFormat("#.###"); rounds to 3 decimal places.

Java Code
import java.text.*;

public class DecimalPlaces {

    public static void main(String[] args) {
        
        double d = 1.234567;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.print(df.format(d));
    }

}



__________________
Don't forget to add code tags around your code:

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote Share this thread on Facebook
The Following 2 Users Say Thank You to JavaPF For This Useful Post:
copeg (09-02-2010), Shambolic (09-02-2010)
Sponsored Links
  #2 (permalink)  
Old 09-02-2010, 04:07 PM
newProgrammer's Avatar
Junior Member
 

Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
newProgrammer is on a distinguished road
Default Re: How to Format a double value to 2 decimal places

Thank you very much!
Reply With Quote
Reply

Tags
decimal places, round number

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to Use Decimal Places napenthia Java Theory & Questions 2 27-04-2009 08:17 AM
How to Type cast convert double to integer JavaPF Java Code Snippets and Tutorials 0 07-04-2009 09:52 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java jtextarea font size programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 12:40 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.