wood burning stoves*
The moose likes Beginning Java and the fly likes getting the right decimal... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting the right decimal..." Watch "getting the right decimal..." New topic
Author

getting the right decimal...

Jeremy Parsons
Ranch Hand

Joined: Apr 21, 2006
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
Dear Jermy

Use the below code,

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


Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
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

Joined: Apr 21, 2006
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

Joined: Oct 13, 2005
Posts: 37879
    
  22
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

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting the right decimal...
 
Similar Threads
Alright, my eyes hurt from staring at this monitor trying to figure this out.
problem with StringBuffer and Double
Logic Errors
Accepting Numeric User Input
DUE AT MIDNIGHT TONIGHT..1 ERROR..HELP