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 9 of 9

Thread: decimal format not working

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default decimal format not working

    import java.util.Formatter;
        import java.util.*; 
        import java.text.DecimalFormat;
     
    public class program5c
        {   
            static DecimalFormat df=new DecimalFormat("#.##"); 
    System.out.printf("%-15s", df.format(number));
    }

    Anyway I can get the printf feature to work with decimal format?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: decimal format not working

    Please post the program's output that shows the problem. Add some comments saying what is wrong with the output and show what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: decimal format not working

    Employee Payroll (Sorted by last name)
    F.Name          L.Name     Hourly Wage     Hours Worked Gross Pay  20% Tax    Net Pay   
    Paul            Allen            $21.50           34       731.0      146.20000000000002 584.8     
    Steve           Ballmer       $18.75           35        656.25     131.25     525.0     
    Bill            Gates            $18.50           40        740.0      148.0      592.0     
    Steve           Jobs          $10.75           25         268.75     53.75      215.0     
    John            Smith         $14.75           30         442.5      88.5       354.0     
     
    Total Hours     Total Gross     Total Net       Highest Net Pay
    164              2838.5              2270.8

    Basically I want all the decimal outputs to be to 2 decimal places. I'm not getting any errors but the program doesn't seem to do that. Also all the numbers are in a string array so I have to use Double.parseDouble to convert them to doubles and then output.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: decimal format not working

    Please post an example of the current output and add some comments to it showing what you want the output to be.
    For example:
    current: 1.23445
    desired: 1.2

    Also post the code that creates the current output.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: decimal format not working

    This is the output:
    Employee Payroll (Sorted by last name)
    F.Name          L.Name     Hourly Wage     Hours Worked Gross Pay  20% Tax    Net Pay   
    Paul            Allen           $21.50           34         731.0      146.20000000000002 584.8     
    Steve           Ballmer         $18.75           35         656.25     131.25     525.0     
    Bill            Gates           $18.50           40         740.0      148.0      592.0     
    Steve           Jobs            $10.75           25         268.75     53.75      215.0     
    John            Smith           $14.75           30         442.5      88.5       354.0     
     
    Total Hours     Total Gross     Total Net       Highest Net Pay
    164.0           $2838.5          $2270.8          Bill Gates

    I want gross pay, tax, and netpay to show 2 decimal places. Example: 731.0 should be 731.00, and 584.8 should be 584.80.



    Code of functin that makes the current output:

     static public void chart(String value, String value2[][])
            {
                System.out.println("Employee Payroll (Sorted by last name)");
    //headings
                System.out.printf( "%-15s %-10s %-15s %-10s %-10s %-10s %-10s", "F.Name", "L.Name", "Hourly Wage", "Hours Worked", "Gross Pay", "20% Tax", "Net Pay" );
                System.out.println();
    //data for each employee
                for(int x = 0; x<=value2.length-2;x++)
                {
     
                    System.out.printf("%-15s %-15s $%-15s %-10s %-10s %-10s %-10s", value2[x][0],value2[x][1], value2[x][2],value2[x][3],value2[x][4],value2[x][5], value2[x][6]);
     
                    System.out.println();
     
                }
     
                System.out.println();
    //total employee values
                System.out.printf( "%-15s %-15s %-15s %-10s", "Total Hours", "Total Gross", "Total Net", "Highest Net Pay" );
                System.out.println();
     
                System.out.printf( "%-15s $%-15s $%-15s %-10s", value2[5][0],value2[5][1],value2[5][2], value);
     
                }
            }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: decimal format not working

    Read the API doc for the DecimalFormat class to see what format characters will give you the minimum fractional digits.

    Where is the DecimalFormat class used in the code that was just posted?

    Which printf() statement and format is wrong? There are too many to easily sort through. Just post the ONE printf that is causing the problem, not all of them.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: decimal format not working

    I found part of the problem, it doesn't add 2 decimal places if there is only 1 decimal place. 8.5 doesn't turn into 8.50. I just need a way to add a 0 if it has only 1 decimal place

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: decimal format not working

    Look at the definition for the format strings in the API doc for the class and method you are using. There are several examples there on different ways to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: decimal format not working

    This works

    DecimalFormat df=new DecimalFormat("#.00");

Similar Threads

  1. Converting decimal number to m minutes, n.nn seconds format.
    By coreysizemore in forum Java Theory & Questions
    Replies: 1
    Last Post: January 20th, 2012, 11:37 PM
  2. Working out the day from numeric date (dd/mm/yy format).
    By ShaunB in forum Java Theory & Questions
    Replies: 6
    Last Post: April 23rd, 2011, 08:55 PM
  3. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  4. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  5. [SOLVED] For loop-ing decimal format
    By Harj in forum Loops & Control Statements
    Replies: 10
    Last Post: October 1st, 2010, 05:47 PM