How to use decimal place when formatting an output?
Sorry, but someone please explain how to use decimal place when formatting an output? also could someone tell me if i am suppos to use an if else statement for my problem.
2) Write a program to compute gross pay. The inputs to your algorithm are the hours worked per week and the hourly pay rate. The rule for determining gross pay is to pay the regular pay rate for all hours worked up to 40, time-and-half for all hours worked over 40 up to 54, and double time forall hours over 54. Compute and display the value for gross pay using this rule. Your program should output hours worked, pay rate, and gross pay. Format your output to display two decimal places.
Re: How to Use Decimal Places
The fastest way is to direct you to here,
JavaPF - Formatting Decimal Places
You will need to use an if/else/elseif statement to decide if the rate is 1.0x 1.5x or 2.0x
Chris
Re: How to Use Decimal Places
ok here is what I got for the second problem, hopefully its done right, if anyone wants to check it and tell me if i need to change anything i would like that
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double rate;
int hours;
double grossPay;
System.out.println("Enter the number of hours worked per week: ");
hours = scan.nextInt();
System.out.println("Enter hourly pay rate: ");
rate = scan.nextDouble();
if(hours<=40){
System.out.println("Hours Worked:" + hours);
System.out.println("Pay Rate:" + rate);
grossPay = rate*hours;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Gross Pay:" + df.format(grossPay));
}
else if(hours>40 && hours<=54){
System.out.println("Hours Worked:" + hours);
System.out.println("Pay Rate:" + rate);
grossPay = (rate*1.5)*hours;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Gross Pay:" + df.format(grossPay));
}
else if(hours>54){
System.out.println("Hours Worked:" + hours);
System.out.println("Pay Rate:" + rate);
grossPay = (rate*2.0)*hours;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Gross Pay:" + df.format(grossPay));
}
}
}