1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
15
Rep Power
0

Java Decimal Places

Hi everyone,

I am new to Java, and I have a simple program to code. I am to receive input of hourly pay, hours worked, and tax rate. Then, I am to calculate the gross pay, the net pay, and the tax amount.

I have had no issue with the above calculation. My problem has to do with the decimal places. I am to have two decimal places, for each gross pay, net pay, and tax amount variables. So, if the pay is \$10, with 20 hours, and a 2% tax rate, my results have to look like \$200.00, \$196.00, and \$4.00.

I have tried different things, but all my results have only yielded one decimal place. If someone can help me figure out a simple line code in order to achieve this, I would appreciate.

Code:
```/** PROGRAM ASSIGNMENT 2     Patrick.java
*
* Author:         Patrick
* Class:          Java, Tuesday and Thursday 9:30am
* Due Date:       February 6, 2014
*
* Program Description:
*   This program is Homework 2. The program prints
*   the author's name and information. It also inputs,
*   calculates and prints total income before tax,
*   total income after tax, and actual tax.
*
*
*/
//import Scanner class from java.util package
import java.util.Scanner;

public class Patrick {
public static void main(String[] args) {
//Print author's information
System.out.println("Patrick, Java Homework 2, Tues/Thur 9:30 am");

//Create a Scanner object
Scanner input = new Scanner(System.in);

System.out.println("Enter hourly pay");
double hourlyPay = input.nextDouble();

System.out.println("Enter hours worked");
int hoursWorked = input.nextInt();

System.out.println("Enter tax rate");
double taxRate = input.nextDouble()/100;

//Calculate total income before tax, total income after tax, actual tax
double grossIncome = hourlyPay * hoursWorked;
double netIncome = grossIncome - (grossIncome * (taxRate));
double taxAmount = grossIncome * taxRate;

//Print outputs
System.out.println("The income before tax is \$" + grossIncome);
System.out.println("The income after tax is \$" + netIncome);
System.out.println("The actual tax is \$" + taxAmount);
}
}```
2. If you want control of how floating point numbers print, look at using the printf() method or the DecimalFormat class.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
15
Rep Power
0
Thank you for your help. I have figured out how to use the printf() method.

I have another problem though. I have an array that prints out 10 random integers on the same line, each separated by a space. And at the end, I have to output an end-of-line symbol.

Here is the relevant part of the problem:

3. Invoke a method named printArray in the same class, to print this array in one line, with a space separating adjacent array elements, and output an end-of-line symbol at the end.
I know that I can print a line by itself using the print() method but I want to know if this possible with printf() after I have printed the last of the 10 integers on the same line.

Here is my printArray code:

public static void printArray(double[] array){
//Print output with two digits after the decimal point
for (int i = 0; i < array.length; i++){
System.out.printf("%4.2f ", array[i]);
}

//End-of-line symbol
System.out.println(" ");
}
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
15
Rep Power
0
I forgot to add that I need to use the printf() method due to the following requirement:

3. You should use printf method to specify the output to have only 2 digits after the decimal point for each double type variable.
5. if this possible
What happened when you tried it?
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
15
Rep Power
0
Originally Posted by NormR
What happened when you tried it?
Well, I can include a print() output method on the line following the printf() statement. Which I did.

However, I just want to know how I can incorporate a newline symbol, \n, in the printf() method so that the output can move to a new line, after all 10 digits have been printed on the same line.
7. how I can incorporate a newline symbol, \n, in the printf() method
Put it inside the "s
Code:
`System.out.printf("%4.2f\n", array[i]);`
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
15
Rep Power
0
Originally Posted by NormR
Put it inside the "s
Code:
`System.out.printf("%4.2f\n", array[i]);`

I get the following output:
Code:
```77.00
75.00
65.00
43.00
43.00
32.00
29.00
17.00
11.00
3.00```

Code:
`77.00 75.00 65.00 43.00 43.00 32.00 29.00 17.00 11.00 3.00`
I guess I am just going to have to settle for the print() method.
9. Only use the \n on the last one and have the last one be outside of the loop.
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
15
Rep Power
0
Originally Posted by NormR
Only use the \n on the last one and have the last one be outside of the loop.
Thanks. I did:

Code:
`System.out.printf("\n");`
It works just fine. I think I will be satisfied with just that. Thanks again, I appreciate.