Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

[HELP] Format to 2 decimal places

$
0
0
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;
   
   
   
  
 }
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>