• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

getting the right decimal...

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Jermy

Use the below code,

NumberFormat frm = new DecimalFormat();
frm.setMaximumFractionDigits(2);
frm.setMinimumFractionDigits(2);
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79656
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's never done THAT before. Explain it to me tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic