-1

I have a class project where my teacher wants me to use type double for my numbers but it is required that I do not use and special functions or formatting when roudning amounts to the nearest dollar.

Here's what I have so far:

//Variable Declarations
    String name;
    int hoursWorked;
    double hourlyPayRate;
    double federalTaxRate;
    double stateTaxRate;
    double grossPay;
    double netPay;
    double totalDeduction;

    //Getting user inputs
    Scanner input = new Scanner(System.in);
    System.out.print("Enter employee's name: ");
    name = input.next();
    System.out.print("Enter number of hours worked in a week: ");
    hoursWorked = input.nextInt();
    System.out.print("Enter hourly pay rate: ");
    hourlyPayRate = input.nextDouble();
    System.out.print("Enter federal tax withholding rate: ");
    federalTaxRate = input.nextDouble();
    System.out.print("Enter state tax withholding rate: ");
    stateTaxRate = input.nextDouble();
    System.out.println();

    //Amount Calculations
    grossPay = hoursWorked * hourlyPayRate;
    totalDeduction = (grossPay * federalTaxRate) + 
            (grossPay * stateTaxRate);

    //Printing Payroll Statement
    System.out.println("Employee Name: " + name);
    System.out.println("Hours Worked: " + hoursWorked);
    System.out.println("Pay Rate: $" + hourlyPayRate);
    System.out.println("Gross Pay: $" + grossPay);
    System.out.println("Deductions: ");
    System.out.println("\tFederal Withholding (" + (federalTaxRate * 100) +
            "%): $" + (grossPay * federalTaxRate));
    System.out.println("\tState Withholding (" + (stateTaxRate * 100) +
            "%): $" + (grossPay * stateTaxRate));
    System.out.println("\tTotal Deduction: $" + totalDeduction);
    System.out.println("Net pay: $" + (grossPay - totalDeduction));

And my output would be something similar to this:

Enter employee's name: Bob
Enter number of hours worked in a week: 25
Enter hourly pay rate: 10.30
Enter federal tax withholding rate: .2
Enter state tax withholding rate: .09

Employee Name: Bob
Hours Worked: 25
Pay Rate: $10.3
Gross Pay: $257.5
Deductions: 
    Federal Withholding (20.0%): $51.5
    State Withholding (9.0%): $23.175
    Total Deduction: $74.675
Net pay: $182.825

I need the outputs on things like federal witholding, state witholding, total deduction and net pay to be rounded to the nearest penny because these are dollar amounts.

  • Possible duplicate of [Double vs. BigDecimal?](http://stackoverflow.com/questions/3413448/double-vs-bigdecimal) – saljuama Jan 26 '16 at 01:37
  • Does you teacher want you to write your own rounding method? – PM 77-1 Jan 26 '16 at 01:37
  • So what **does** your teacher want you to user? And what constitutes a "special function"? – Hovercraft Full Of Eels Jan 26 '16 at 01:38
  • stupid assignment, really – Scott Sosna Jan 26 '16 at 01:40
  • not such a stupid assignment. I'll only tell you how to do it, since this is an assignment, you have to program it for yourself. You first have to find the part left and right of the dot, so repeatedly divide your number by ten until it's smaller than 0. Now you have the fractional part. If the fractional part >= 0.5 you add 1 to your number and subtract the fractional part, else you just subtract the fractional part. – johannes_lalala Jan 26 '16 at 01:50
  • Two quotes from you: "when roudning amounts to the nearest dollar" and "to be rounded to the nearest penny". What is it, rounding to dollars or to pennies? Please be clear in your questions. – Erwin Bolwidt Jan 26 '16 at 02:25

2 Answers2

0

The simplest approach to rounding a positive double to the nearest int without using library functions is:

static int round(double d) {
    return (int)(d + .5);
}

This works because casting a floating point number to an int truncates the non-integer part of the number.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

What you need to know is the number in the thousandths decimal place. The way I would get that is first move the decimal to where you want to round. (shifted = netPay * 100)

Next you need to get rid of everything left of the decimal. I would take advantage of type casting. (thousandths = shifted - (int)shifted)

Then you have a simple if statement to know whether to round up or down. To round down it's (netPay = netPay - (thousandths / 100))

To round up it's (netPay = netPay + ((1 - thousandths) /100)

I hope this helps.

Brian
  • 307
  • 2
  • 15