7 Replies - 488 Views - Last Post: 27 January 2013 - 09:39 PM Rate Topic: ***** 1 Votes

#1 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

[HELP] Format to 2 decimal places

Posted 27 January 2013 - 07:24 PM

Can someone check my code, everything is working fine but the decimal is not formatting to 2 decimal places. I've written a code to format it to 2 decimal places at the end but its not working.

import java.util.Scanner;
 public class PayrollStatementC{
  public static void main(String[] args){
  
  //Create a scanner
  Scanner input=new Scanner(System.in);
  
   //Enter Employee's name
   System.out.print("\nEnter employee's name: ");
   String EmployeeName= input.next();
   
   //Enter number of hours worked in a week
   System.out.print("Enter number of hours worked in a week: ");
   double HoursWorked=input.nextDouble();
   
   //Enter hourly pay rate
   System.out.print("Enter hourly pay rate: ");
   double PayRate=input.nextDouble();
   
   //Enter federal tax 
   System.out.print("Enter federal tax withholding rate: ");
   double FederalTax=input.nextDouble();
   
   //Enter state tax
   System.out.print("Enter state tax withholding rate: ");
   double StateTax=input.nextDouble();
   
   //Display employee name
   System.out.println("\nEmployee name: "+EmployeeName);
    
   //Display hours worked
   System.out.println("Hours worked: "+HoursWorked);
   
   //Display hourly payrate
   System.out.println("Pay Rate: "+PayRate);
   
   //Calculate gross pay
   double GrossPay=HoursWorked*PayRate;
   
   //Display gross pay
   System.out.println("Gross Pay: "+GrossPay);
   
   //Display deductions
   System.out.println("Deductions:");
   
   //Calcuate federal tax 
   double TotalFederalTax=(FederalTax/100)*GrossPay;
   
   //Display federal tax 
   System.out.println("  Federal Withholding ("+FederalTax+"%): "+TotalFederalTax);
   
   //Calculate state tax
   double TotalStateTax=(StateTax/100)*GrossPay;
   
   //Display state tax
   System.out.println("  State Withholding ("+StateTax+"%): "+TotalStateTax);
   
   //Calculate total deductions
   double TotalDeductions=TotalFederalTax+TotalStateTax;
   
   //Display total deductions
   System.out.println("  Total Deductions: "+TotalDeductions);
   
   //Calculate net pay
   double NetPay=GrossPay-TotalDeductions;
   
   //Display net pay
   System.out.println("Net Pay: "+NetPay);
   
   //Format to keep 2 digits after decimal
   TotalFederalTax=(int)(TotalFederalTax*100)/100.0;
   TotalStateTax=(int)(TotalStateTax*100)/100.0;
   TotalDeductions=(int)(TotalDeductions*100)/100.0;
   NetPay=(int)(NetPay*100)/100.0;
   
   
   
  
 }
}

This post has been edited by magicm00n: 27 January 2013 - 07:24 PM


Is This A Good Question/Topic? 1
  • +

Replies To: [HELP] Format to 2 decimal places

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8959
  • View blog
  • Posts: 33,585
  • Joined: 12-June 08

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 07:31 PM

I am not sure where you are doing that at, but I was under the impression 'decimalformatt' works best.
Was This Post Helpful? 0
  • +
  • -

#3 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 07:37 PM

Towards the end. Here:

//Format to keep 2 digits after decimal
TotalFederalTax=(int)(TotalFederalTax*100)/100.0;
TotalStateTax=(int)(TotalStateTax*100)/100.0;	   
TotalDeductions=(int)(TotalDeductions*100)/100.0;
NetPay=(int)(NetPay*100)/100.0;

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8959
  • View blog
  • Posts: 33,585
  • Joined: 12-June 08

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 07:41 PM

Hmm.. yeah.. not sure what was going on with that. Use the 'decimal formatter' I linked above.

Side note - are you trying to get the int value cast of the whole mess of numbers there or just the first part?
//what you have looks to be casting the "(TotalFederalTax*100)" as int then dividing by 100
(int)(TotalFederalTax*100)/100.0;

//or do you want to do the math first then cast the whole "(TotalFederalTax*100)/100.0" first to int?
(int)((TotalFederalTax*100)/100.0);

Was This Post Helpful? 0
  • +
  • -

#5 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 07:44 PM

I will try that link thanks. Btw that method is just a trick by my lecturer to format it to 2 decimal places. We haven't learned the official way yet but I will try it with the link you gave ^^/>/>.

It works with a Dialog Box but it doesn't work with the console.

This post has been edited by magicm00n: 27 January 2013 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • View blog
  • Posts: 3,039
  • Joined: 05-April 11

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 08:55 PM

You are printing out the value and THEN you format it to two decimal places

Change it around so you first format it to two decimal places, and then print it out :)/>
You only need to move your println's a couple of lines down
import java.util.Scanner;
 public class PayrollStatementC{
  public static void main(String[] args){
  
  //Create a scanner
  Scanner input=new Scanner(System.in);
  
   //Enter Employee's name
   System.out.print("\nEnter employee's name: ");
   String EmployeeName= input.next();
   
   //Enter number of hours worked in a week
   System.out.print("Enter number of hours worked in a week: ");
   double HoursWorked=input.nextDouble();
   
   //Enter hourly pay rate
   System.out.print("Enter hourly pay rate: ");
   double PayRate=input.nextDouble();
   
   //Enter federal tax 
   System.out.print("Enter federal tax withholding rate: ");
   double FederalTax=input.nextDouble();
   
   //Enter state tax
   System.out.print("Enter state tax withholding rate: ");
   double StateTax=input.nextDouble();
   
   //Calculate gross pay
   double GrossPay=HoursWorked*PayRate;
   
   //Calcuate federal tax 
   double TotalFederalTax=(FederalTax/100)*GrossPay;
   
   //Calculate state tax
   double TotalStateTax=(StateTax/100)*GrossPay;
   
   //Calculate total deductions
   double TotalDeductions=TotalFederalTax+TotalStateTax;
   
   //Calculate net pay
   double NetPay=GrossPay-TotalDeductions;
   
   //Format to keep 2 digits after decimal
   TotalFederalTax=(int)(TotalFederalTax*100)/100.0;
   TotalStateTax=(int)(TotalStateTax*100)/100.0;
   TotalDeductions=(int)(TotalDeductions*100)/100.0;
   NetPay=(int)(NetPay*100)/100.0;
   
   //Display employee name
   System.out.println("\nEmployee name: "+EmployeeName);
    
   //Display hours worked
   System.out.println("Hours worked: "+HoursWorked);
   
   //Display hourly payrate
   System.out.println("Pay Rate: "+PayRate);
   
   //Display gross pay
   System.out.println("Gross Pay: "+GrossPay);
   
   //Display deductions
   System.out.println("Deductions:");
   
   //Display federal tax 
   System.out.println("  Federal Withholding ("+FederalTax+"%): "+TotalFederalTax);
   
   //Display state tax
   System.out.println("  State Withholding ("+StateTax+"%): "+TotalStateTax);
   
   //Display total deductions
   System.out.println("  Total Deductions: "+TotalDeductions);
   
   //Display net pay
   System.out.println("Net Pay: "+NetPay);
 }
}





Quote

Side note - are you trying to get the int value cast of the whole mess of numbers there or just the first part?

It is fine, but you might run into double overflow problems if you are working with very big numbers ^^
check the follow example
double value = 20.534;
value = (int) value*100; //2053 will be the current number after cast
value /= 100; //20.53 will be the final result


This post has been edited by CasiOo: 27 January 2013 - 08:59 PM

Was This Post Helpful? 1
  • +
  • -

#7 magicm00n  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 114
  • Joined: 01-March 12

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 09:29 PM

Thank you!!! Re-arranging it works ^^.
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • View blog
  • Posts: 3,039
  • Joined: 05-April 11

Re: [HELP] Format to 2 decimal places

Posted 27 January 2013 - 09:39 PM

View Postmagicm00n, on 28 January 2013 - 04:29 AM, said:

Thank you!!! Re-arranging it works ^^/>.


Sometimes it is easy to miss the simple mistakes =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1