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

# Thread: How to use decimal place when formatting an output?

1. ## 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.

2. ## 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

3. ## 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));
}
}
}