PDA

View Full Version : [Java] Help with number formatting


gxs
11-06-2006, 12:29 AM
I've 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 professor hasn't gone over how to format the output how I want, and doesn't 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, so it is more clear for me?

Here's what I have:


/* Program Description: A Java program that calculates the wages for a company’s employees.
The first 20 hours above 40 are to be paid at the overtime rate of
1.5 times the base rate. Hours above 60 are to be paid at 2 times
the base rate.


*/

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");
}
}

Gox
11-06-2006, 05:18 AM
You could try the printf method. http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)

Something like...
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.

gxs
11-06-2006, 07:46 AM
Thanks a lot for the help, here's my final code:


/* Program Description: A Java program that calculates the wages for a company’s employees.
The first 20 hours above 40 are to be paid at the overtime rate of
1.5 times the base rate. Hours above 60 are to be paid at 2 times
the base rate.


*/

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 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 Enter your employee number: ");
employeeNumber = input.nextDouble();


System.out.print("\n Enter your base rate of pay per hour: ");
payRate = input.nextDouble();


System.out.print("\n 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) + ((20) * (payRate * 1.5)) + ((hoursWorked - 60) * (payRate * 2));
wagesTotal += amountEarned;

}



System.out.printf("\n\n\tEmployee ID#: %.0f ", employeeNumber);

System.out.printf("\n\tTotal hours worked: %.2f ", hoursWorked);

System.out.printf("\n\tPay rate: $ %.2f per hour", payRate);

System.out.printf("\n\tTotal wages earned by employee: $ %.2f ", amountEarned);

} // end for loop

System.out.printf("\n\n\t Total wages earned by all employees: $ %.2f \n\n", wagesTotal);
System.out.print("Program made by: \n<my name>\n\n");
}
}

Aradon
11-07-2006, 12:43 AM
Just as a future reference, the printf method is def. the shortest way and probably the quickest however if you want a little more formatting with locale (to which I know this isn't part of this particular problem but for future reference) you can use the DecimalFormat as well

DecimalFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html)