Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Correct Number formatting

    I have tried to figure out how to implement the java.text.DecimalFormat import, but I couldn't figure out the correct syntax to apply it to my numbers. This program is for an entry level programming class. The application require it actually be formatted, but I want to know how to anyway.

    I have an employee ID number that I need formatted without any decimal places, the dollar amounts to have two decimals places, and the employee hours to only have decimal places.
    Can anyone maybe explain the syntax for any particular variable I am using

    Here's what I have:

    Java Code:
    import java.util.Scanner;
    
    public class FigureWages {
        
        public static void main(String[] args) {
            
            double employeeNumber;
            double payRate;
            double wagesTotal; // Total wages of all employees
            double amountEarned;
            double hoursWorked;
            double formTotal; //loop counter
            String employeeTotalString;
            double employeeTotal;
            
            
            Scanner input = new Scanner(System.in);
            
        
    
            do{
                System.out.print("\n Please enter the total number of employees: ");
                employeeTotalString = input.nextLine();
                employeeTotal = Double.parseDouble(employeeTotalString);
                
                }while (employeeTotal <= 0);// end do-while loop
    
            
            for (formTotal = employeeTotal, amountEarned = 0, wagesTotal = 0; formTotal > 0; formTotal--, employeeTotal--) {
                
    
                System.out.print("\n\n    Please enter your employee number:");
                employeeNumber = input.nextDouble();
                
    
                System.out.print("\n    Please enter your base rate of pay per hour: ");
                payRate = input.nextDouble();
                
    
                System.out.print("\n    Please enter your number of hours worked: ");
                hoursWorked = input.nextDouble();
                
                //overtime calculations
                if (hoursWorked <= 40){
                    amountEarned = payRate * hoursWorked;
                    wagesTotal += amountEarned;    
    
                }
                else if (hoursWorked > 40 && hoursWorked <=60){
                    amountEarned = (40 * payRate) + ((hoursWorked - 40) * (payRate * 1.5));
                    wagesTotal += amountEarned;    
        
                }
                else if (hoursWorked >= 60){
                    amountEarned = (40 * payRate) + ((hoursWorked - 40) * (payRate * 1.5)) + (hoursWorked * 2);
                    wagesTotal += amountEarned;
                    
                }
                
                System.out.print("\n\n\tEmployee ID#: " + employeeNumber);
                
                System.out.print("\n\tTotal hours worked: " + hoursWorked);
                
                System.out.print("\n\tPay rate: "+ payRate + " (per hour)");    
            
                System.out.print("\n\tTotal wages earned by employee: " + "$" + amountEarned + "\n\n\n");
                
    
            }// end for loop
        
        System.out.print("\n\t     Total wages earned by all employees: " + "$" + wagesTotal + "\n\n\n");
    }
    }
    Thanks

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    You could try the printf method. PrintStream (Java 2 Platform SE 5.0)

    Something like...
    Java Code:
    System.out.printf("\n\t Total wages earned by employee: $ %.2f \n\n\n", amountEarned);
    I not sure the above is proper syntax and I don't have the means to check at the moment.

    Greetings.

Similar Threads

  1. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 07:11 PM
  2. Formatting a number to currency
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-16-2008, 10:59 PM
  3. Is my Pseudocode correct?
    By Clemenza1983 in forum New To Java
    Replies: 0
    Last Post: 01-29-2008, 04:07 AM
  4. formatting String
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 07:27 PM
  5. Formatting the date
    By yuchuang in forum New To Java
    Replies: 5
    Last Post: 05-07-2007, 06:08 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
  •