Results 1 to 10 of 10
  1. #1
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Formatting this output to show "#.##"

    How can I get the output of this program to display:

    148.41 instead of 148.4131591025766

    Java Code:
    [COLOR="Blue"]public class[/COLOR] Test
    {
        [COLOR="blue"]public static double[/COLOR] calculate([COLOR="blue"]final double[/COLOR] x, [COLOR="blue"]final double[/COLOR] y)
    {
            [COLOR="blue"]class[/COLOR] Helper
            {
                [COLOR="blue"]double[/COLOR] doSomething()
                {
                    [COLOR="blue"]return[/COLOR] Math.sqrt(x) + Math.sqrt(y);
                }
            }
            Helper h = [COLOR="Blue"]new[/COLOR] Helper();
            [COLOR="blue"]return[/COLOR] Math.exp(h.doSomething());
        }
        [COLOR="blue"]public static void[/COLOR] main(String[] args)
        {
     System.[COLOR="SeaGreen"]out[/COLOR].println(calculate(4.0, 9.0));
        }
    }

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    10

    Default

    Hi Twiggy

    You can achieve truncation by multiplying, casting and dividing your number as follows:
    Java Code:
        public static void main(String[] args)
        {
            double answer = (double) ((int) (calculate(4.0, 9.0) * 100d)) / 100d;
            System.out.println(answer);
        }
    Check the Math.round() method as well. ;)

    Goodluck
    Tim
    Last edited by tim; 02-07-2010 at 03:07 PM. Reason: Casting from double precision to integer precision causes truncation.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,395
    Blog Entries
    7
    Rep Power
    25

    Default

    Quote Originally Posted by tim View Post
    Hi Twiggy

    You can achieve rounding by multiplying, casting and dividing your number
    Don't do that: use a Format object to do the job; the printf( ... ) method uses those objects for its format specifiers; e.g.

    Java Code:
    System.out.printf("%.3f", 4.123456);
    kind regards,

    Jos

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Don't do that: use a Format object to do the job; the printf( ... ) method uses those objects for its format specifiers; e.g.

    Code:
    System.out.printf("%.3f", 4.123456);
    kind regards,
    I had similar problem, am I correct to think that the output will be 4.123?

    Thanks
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,395
    Blog Entries
    7
    Rep Power
    25

    Default

    Quote Originally Posted by FlyNn View Post
    I had similar problem, am I correct to think that the output will be 4.123?

    Thanks
    Yep, but don't take my word for it but try it instead.

    kind regards,

    ps. I like your signature.

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Yeah that was my next step. I should of tried it before though. My mistake.
    I am glad you like the signature, thanks.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    If you like printf (and I do), you can get the same result with String#format and also Formatter. The latter two are useful if you want the formatted number to appear in a non-console environment such as a Swing app.

  8. #8
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    After reviewing both proposed solutions, the real answer for me is using a combination of both.

    Java Code:
    public class Test {
    
        public static double calculate(final double x, final double y) {
            class Helper {
    
                double doSomething() {
                    return Math.sqrt(x) + Math.sqrt(y);
                }
            }
            Helper h = new Helper();
            return Math.exp(h.doSomething());
        }
    
        public static void main(String[] args) {
            System.out.println(calculate(4.4, 9.2));
            double answer = (double) ((int) (calculate(4.4, 9.2) * 1000d)) / 1000d;
            System.out.println(answer);
            System.out.printf("%.2f", answer);
        }
    }
    Gives me the following results:

    run:
    169.1469517303579
    169.146
    169.15
    BUILD SUCCESSFUL (total time: 1 second)


    However to get these results, for the Format object, I have to multiply by 1,000 instead of 100, and if I wanted to round to nearest 100th, then I would have to multiply by 10,000, nearest 1000th multiply by 100,000, etc, etc.
    My final solution would look like this:

    Java Code:
    public class Test {
    
        public static double calculate(final double x, final double y) {
            class Helper {
    
                double doSomething() {
                    return Math.sqrt(x) + Math.sqrt(y);
                }
            }
            Helper h = new Helper();
            return Math.exp(h.doSomething());
        }
    
        public static void main(String[] args) {
            double answer = (double) ((int) (calculate(4.0, 9.0) * 1000d)) / 1000d;
            System.out.printf("%.2f", answer);
        }
    }
    With these results:

    run:
    148.41
    BUILD SUCCESSFUL (total time: 1 second)


    Thanks for all contributions! :)
    Last edited by twiggy62; 02-06-2010 at 06:31 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,395
    Blog Entries
    7
    Rep Power
    25

    Default

    Quote Originally Posted by twiggy62 View Post
    The first reply works best in my case, because I am not simply trying to format a number (using the Format object), I am also performing a calculation at the same time, so I don't know what the result is until the calculations are performed.
    Java Code:
    System.out.printf("result: %.3f", <very_complicated_calculation>);
    ?

    kind regards,

    Jos

  10. #10
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Oh, I see where you are coming from, you are right. I was just over thinking it.

    My solution would then look like this:

    Java Code:
    public class Test {
    
        public static double calculate(final double x, final double y) {
            class Helper {
    
                double doSomething() {
                    return Math.sqrt(x) + Math.sqrt(y);
                }
            }
            Helper h = new Helper();
            return Math.exp(h.doSomething());
        }
    
        public static void main(String[] args) {
            System.out.printf("%.2", calculate(4.0, 9.0));
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  2. Replies: 2
    Last Post: 10-08-2009, 11:28 PM
  3. output "left click"/enter command?
    By Arsenic in forum New To Java
    Replies: 2
    Last Post: 06-04-2009, 05:01 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •