Results 1 to 2 of 2
Thread: Correct Number formatting
- 07-16-2007, 04:53 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
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:
ThanksJava 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"); } }
- 08-07-2007, 04:59 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 39
- Rep Power
- 0
You could try the printf method. PrintStream (Java 2 Platform SE 5.0)
Something like...
I not sure the above is proper syntax and I don't have the means to check at the moment.Java Code:System.out.printf("\n\t Total wages earned by employee: $ %.2f \n\n\n", amountEarned);
Greetings.
Similar Threads
-
formatting..
By sireesha in forum New To JavaReplies: 16Last Post: 06-26-2009, 07:11 PM -
Formatting a number to currency
By Java Tip in forum java.textReplies: 0Last Post: 04-16-2008, 10:59 PM -
Is my Pseudocode correct?
By Clemenza1983 in forum New To JavaReplies: 0Last Post: 01-29-2008, 04:07 AM -
formatting String
By bugger in forum New To JavaReplies: 1Last Post: 11-16-2007, 07:27 PM -
Formatting the date
By yuchuang in forum New To JavaReplies: 5Last Post: 05-07-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks