hi, I'm trying to do my decimal to the nearest hundreths, i've tried using number format and printf but i couldn't do it. Anybody can help me with this: here is my program

float grossPay = (calcGross(nHrsWrkd, hrlyRate));
   float netPay = (calcTaxes (calcGross (nHrsWrkd, hrlyRate)));

   System.out.print("\nTaxes Withheld - $"
           +  nf.format(calcTaxes  (grossPay)) );
    System.out.print("\nNet Pay        - $" +
          nf.format(grossPay - netPay));

 static float calcTaxes(float grossPay)///calc of income tax
{

float netPay = 0 ;
final float x = 0.15f;
final float y = 0.20f;
final float z = 0.25f;
final float a = 300;
final float b = 450;
final float c = 451;

 if (grossPay >= c)
 netPay = (float)((z * (grossPay - b)) + (x * a) + (y * (b - a)));
 if (grossPay <= b)
netPay = (float) (y *(grossPay - a) + (x * a));
else if (grossPay <= a)
 netPay =   (float) (grossPay - (grossPay * z));
 return netPay ;


}

i'm trying to input the data like this

hrs work 52.50
hr rate     15.00
gross pay 881.25

output=

tax withheld = 182.812
net pay         = 698.438

and i'm getting an output of more than 2 decimal place eventhough i applied the number format command

Recommended Answers

All 6 Replies

nf.format(calcTaxes (grossPay))

How do you define the "nf"?

I would suggest to write a simple program with only a double number and the Formatter, and try to print it the way you want. Once you accomplish that, transfer the Formatter to your main method

NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
this is the way i define the nf syntax and im still getting more than 2 decimal digit

this is the main body of the program

    static Scanner input = new Scanner (System.in);

    public static void main(String[] args){

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(2);
        //input emloyess first, last name, # hrs worked, hrly rate
    do{
    System.out.print("\nPlease enter the employee's first name     - ");

    String fname = input.next();

    System.out.print("\nNow please enter the employee's last name  - ");
    String lname = input.next();

    System.out.print("\nEnter the number of hours " +  (fname) + " " +( lname)
            +" worked this week - ");
    float nHrsWrkd ;
    nHrsWrkd = input.nextFloat();
    System.out.print("\nFinally, please enter the hourly rate earned by "
           + (fname) + " " + (lname)+ " - ");
   float hrlyRate;
   hrlyRate = input.nextFloat();
   System.out.print("\nEmployee Name  - " + fname + " " + lname);
   System.out.print("\nHours Worked   - " + nf.format( nHrsWrkd));
   System.out.print("\nHourly Rate    - $" + nf.format(hrlyRate));
   System.out.print("\nGross Pay      - $"
           + nf.format ( calcGross (nHrsWrkd,hrlyRate)));

   float grossPay = (calcGross(nHrsWrkd, hrlyRate));
   float netPay = (calcTaxes (calcGross (nHrsWrkd, hrlyRate)));

   System.out.print("\nTaxes Withheld - $"
           +  nf.format(calcTaxes  (grossPay)) );
    System.out.print("\nNet Pay        - $" +
          nf.format(grossPay - netPay));

} while (doYouWish());/// option to terminate the program
    System.out.println("\nGood Bye!!");


    }

Why don't you try:
DecimalFormat ?

i'm not familiar using that command...Could you give me some tricks of using it

i'm not familiar using that command...Could you give me some tricks of using it

In your code you have been using NumberFormat. It's the same thing. Have you looked at the link? Just read the API and use the methods. Experiment a little.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.