Results 1 to 18 of 18
  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default How to print decimals.

    Hi guys, I am making a small program that converts meters to feet.
    I want my answer to be printed with 2 decimals, but I am struggelig.
    This is my current code, any takes on what needs to be changed and why?

    Thanks for the help!:)

    import java.util.Scanner;
    import java.text.DecimalFormat;

    public class Chapter2PE3 {
    public static void main (String[]args){

    Scanner input = new Scanner (System.in);


    System.out.println("Type in feet");
    double feet = input.nextDouble();

    //Calculate
    double meters = feet * 0.305;

    DecimalFormat df = new DecimalFormat ("#,##");
    System.out.println(feet + " feet is " + df.format(meters) + "meters");



    }
    }

  2. #2
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: How to print decimals.

    What you should be passing to DecimalFormat when it is created is "#.00" That should fix your problem.

  3. #3
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: How to print decimals.

    Alternatively
    Java Code:
    System.out.println( String.format( "%.2f feet is %.2f meters", feet, meters ) );

  4. #4
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by jbarke12 View Post
    What you should be passing to DecimalFormat when it is created is "#.00" That should fix your problem.
    Thank you for you reply, if I change: DecimalFormat df = new DecimalFormat ("#,##"); to DecimalFormat df = new DecimalFormat ("#.00");

    It prints the answer I am looking for great! Thank you very much! What is the reason for when to use ,## or .00 ?

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

    Default Re: How to print decimals.

    According to the API documentation, the # indicates an optional decimal, while 0 always prints a digit (even if it equals zero).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by JosAH View Post
    According to the API documentation, the # indicates an optional decimal, while 0 always prints a digit (even if it equals zero).

    kind regards,

    Jos
    Great! Thank you.

    If I want the decimals to round to the closes 0.50? Can I still use "DecimalFormat" or will I need to do something different?
    This is something that will be very helpful when making programs where the output is in dollars.

    Thanks for all the help! :)

  7. #7
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: How to print decimals.

    DecimalFormat doesn't round for you. Math Round rounds to the nearest whole number. I don't know of an API that rounds to the nearest half. Its probably a method you would have to create.

  8. #8
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: How to print decimals.

    Math.round(value - 0.5) + 0.5 would do what you want

  9. #9
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by jbarke12 View Post
    Math.round(value - 0.5) + 0.5 would do what you want
    Great that works perfectly.
    I guess it works like this : 11.76-0.50 = 11.26, gets rounded to 11 and then you add back on the 0.50?

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to print decimals.

    So, even if you have a number like 11.99 you want to round it to 11.50?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by jbarke12 View Post
    Math.round(value - 0.5) + 0.5 would do what you want
    Great that works perfectly.
    I guess it works like this : 11.76-0.50 = 11.26, gets rounded to 11 and then you add back on the 0.50?
    Then it will end up rounding to 11.50 even tho the correct answer is 12.00

  12. #12
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    No, I want to round it to the closest 0.50 , 1.0 , 1.50 and so on.
    So 11.99 I would like to round to 12.00.

    "Great that works perfectly.
    I guess it works like this : 11.76-0.50 = 11.26, gets rounded to 11 and then you add back on the 0.50?"

    Will actually not work, since in the situation above, it will round to 11.50 even tho the closes 0.5 increment is 12.00.

    Any suggestions?

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

    Default Re: How to print decimals.

    You can make a BigDecimal round 'half even', 'half up' or 'half down'.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by JosAH View Post
    You can make a BigDecimal round 'half even', 'half up' or 'half down'.

    kind regards,
    Thank you JosAH, I have never used BigDecimal in my code before, how will I use it my code?
    If this is the line I want to have rounded: System.out.println(total);
    i.e the double total is the variable I want to round to the closes 0.5 increment.

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

    Default Re: How to print decimals.

    Have you read the API documentation for the BigDecimal class?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by JosAH View Post
    Have you read the API documentation for the BigDecimal class?

    kind regards,

    Jos
    I have not, I have never used BigDecimal before in my coding.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to print decimals.

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: How to print decimals.

    Quote Originally Posted by Tolls View Post
    Thank you! :)
    Thanks for all the help guys!

Similar Threads

  1. Decimals
    By Goughyy_ in forum New To Java
    Replies: 3
    Last Post: 04-21-2012, 06:26 PM
  2. Help with decimals
    By sabarooni in forum New To Java
    Replies: 4
    Last Post: 11-02-2011, 02:07 AM
  3. Replies: 2
    Last Post: 07-31-2011, 11:19 PM
  4. BIG decimals
    By xael in forum New To Java
    Replies: 17
    Last Post: 10-04-2010, 10:14 AM
  5. get more decimals?!?! please help!
    By michcio in forum New To Java
    Replies: 7
    Last Post: 05-22-2008, 10:26 PM

Posting Permissions

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