Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# getting the right decimal...

Jeremy Parsons
Ranch Hand
Posts: 74
I have been working with an online class and have this project to complete. I have everything working, Except the deicmals.. (ie. if I have \$478.00 I only get \$478.0 printed). What do I do to get the other zero?.. Below I have attached my code incase I've over looked something..

class payRoll
{
public static void main (String [] args)
{
double perHour = 12.00,overTimeHour = 5.5, worked = 45.5, withHolding;
int hoursNormal = 40;
double overTimePay = 18.00,regularPay, grossPay, netPay, taxRate = .285 , ot;
String employeeName;
employeeName = "John Smith"; //declares String "employeeName" the value of "John Smith"

grossPay = perHour * hoursNormal; //takes 40 hours and multiplies \$12.00/hr
ot = overTimeHour * overTimePay; //takes 5.5 hours and multiplies \$18.00/hr
regularPay = grossPay + ot; //takes both equasions and adds them up for the gross pay
withHolding = taxRate * regularPay; //takes the gross pay and multiplies by .285 or 28.5% for the withholding
netPay = regularPay - withHolding; //takes gross pay and subtracts the withholding total to get net pay

System.out.print("Employee name: ");
System.out.println(employeeName);
System.out.print("Hours Worked: ");
System.out.println(worked);
System.out.print("Hours Regular: ");
System.out.println("40 x \$12.00 = \$" + grossPay);
System.out.print("Hours Overtime: ");
System.out.println("5.5 x \$18.00 = \$" + ot);
System.out.print("Gross Pay:\t\t\t\b\b\$");
System.out.println(regularPay);
System.out.println();
System.out.print("Withholdings:\t\t\t\b\b\b(\$");
System.out.println(withHolding + ")");
System.out.println();
System.out.print("Net Pay:\t\t\t\b\b\b\$");
System.out.println(netPay);

}
}

any help will be greatly appreciated..

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15574
46
The thing you're missing is number formatting.

If you're using Java 5, use System.out.printf(...) and use a format pattern to specify that you want two decimal places. Lookup the printf(...) method in the Java API documentation.

If you're using an older version of Java, you'll need to use classes like java.text.DecimalFormat (because older versions of Java didn't have the printf method yet).

KasiMurugan Ramasamy
Ranch Hand
Posts: 125
Dear Jermy

Use the below code,

NumberFormat frm = new DecimalFormat();
frm.setMaximumFractionDigits(2);
frm.setMinimumFractionDigits(2);

KasiMurugan Ramasamy
Ranch Hand
Posts: 125
Dear Jermy

Use the below code,

NumberFormat frm = new DecimalFormat();
frm.setMaximumFractionDigits(2);
frm.setMinimumFractionDigits(2);
frm.format(doubleValue) it returns string.

Jeremy Parsons
Ranch Hand
Posts: 74
when I put the code in, it won't compile, and I get the following..

C:\WINDOWS\Desktop\payRoll.java:25: cannot find symbol
symbol : class NumberFormat
location: class payRoll
NumberFormat frm = new DecimalFormat();
^
C:\WINDOWS\Desktop\payRoll.java:25: cannot find symbol
symbol : class DecimalFormat
location: class payRoll
NumberFormat frm = new DecimalFormat();
^
C:\WINDOWS\Desktop\payRoll.java:28: cannot find symbol
symbol : variable doubleValue
location: class payRoll
frm.format(doubleValue);
^
3 errors

Tool completed with exit code 1

Is there an easier way to get that extra decimal to show?

Campbell Ritchie
Sheriff
Posts: 50749
83
Yes, Jesper Young has told you an easier way. Look in the API specification for the Formatter class and read about the % tags; there are hundreds of them but you can ignore most of them. The printf method uses exactly the same tags.
That will get you out of using the awkward NumberFormat classes.

As for NumberFormat (which I wouldn't use myself) and your compiler error.
There are lots of websites which help you. This one has lots of compiler error messages in. Read it. It mentions that you might have to import the package which the classes are in.
[ June 26, 2006: Message edited by: Campbell Ritchie ]

Layne Lund
Ranch Hand
Posts: 3061
The link to the API docs that Campbell gave above can also help you find which package to import for the NumberFormat class. You should take the time to learn how to navigate these docs. They are a very valuable tool when programming in Java.

Layne

Nigel Browne
Ranch Hand
Posts: 703
You may also find that reading the Internationalization trail of the java tutorial is useful as it has a lesson on formating. Follow this link.