Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Rounding Question

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rounding Question

    Hey everybody!

    How would I convert the output given to this variable (which is double) into a number rounded to teh nearest 2 numbers after the decimal point.

    PST = ((TRSA * (1-SD/100) * PST));

    I need it to the nearest number after two decimal points as it's having to do with money.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Rounding Question

    public class z
    {
        public static void main(String [] args)
        {
            double x = 3.14159265358979323846;
            System.out.printf("x = %.16f\n", x);
            System.out.printf("x = %.2f\n\n", x);
     
            double y = 2.7182818284590452354;
            System.out.printf("y = %.16f\n", y);
            System.out.printf("y = %.2f\n", y);
        }
    }


    Output:

    x = 3.1415926535897930
    x = 3.14

    y = 2.7182818284590450
    y = 2.72


    Cheers!

    Z
    Last edited by Zaphod_b; July 23rd, 2012 at 02:31 PM.

Similar Threads

  1. [SOLVED] Double rounding
    By krillov in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 12th, 2012, 01:09 PM
  2. [SOLVED] Rounding Decimal Numbers Question
    By Nuggets in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2012, 04:12 PM
  3. Question about rounding
    By CoolGuy134 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 27th, 2011, 04:30 PM
  4. Rounding the numbers
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 12:45 AM
  5. Rounding an int. Help please
    By Akim827 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2010, 12:36 AM