Forums Register Login

getting the right decimal...

+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
Dear Jermy

Use the below code,

NumberFormat frm = new DecimalFormat();
frm.setMaximumFractionDigits(2);
frm.setMinimumFractionDigits(2);
+Pie Number of slices to send: Send
Dear Jermy

Use the below code,

NumberFormat frm = new DecimalFormat();
frm.setMaximumFractionDigits(2);
frm.setMinimumFractionDigits(2);
frm.format(doubleValue) it returns string.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Get off me! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 994 times.
Similar Threads
Logic Errors
Accepting Numeric User Input
problem with StringBuffer and Double
Alright, my eyes hurt from staring at this monitor trying to figure this out.
DUE AT MIDNIGHT TONIGHT..1 ERROR..HELP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:22:58.