I am having an issue with rounding on a c# Payroll program.
I tested it with 15.50 hourly rate x 39.75 hours. This comes to $616.125 gross pay, rounded up to $616.13 gross.
Then I did a fixed Income tax of 25%, which means the final Net Pay of (15.50 x 39.75) x .25 = 462.10
However it keeps displaying a Net Pay of 462.09, so it must not be using the rounded gross pay for the display part. The Gross Pay and Income tax are displaying correctly
Here is all of the code, my guess is that this line needs to be changed among others:
decimal incomeTax = Math.Round(grossPay, 3) * taxRate;
Any ideas what I need to do to get it to round correctly?
decimal hourlyRate = 0;
decimal hoursWorked = 0;
decimal grossPay = 0m;
decimal incomeTax = 0m;
decimal netPay = 0m;
decimal taxRate = .25m;
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal hourlyRate = Convert.ToDecimal(txtHourlyRate.Text);
decimal hoursWorked = Convert.ToDecimal(txtHoursWorked.Text);
decimal grossPay = Math.Round(hourlyRate * hoursWorked, 3);
decimal incomeTax = Math.Round(grossPay, 3) * taxRate;
decimal netPay = grossPay - incomeTax;
txtGrossPay.Text = grossPay.ToString("c");
txtIncomeTax.Text = incomeTax.ToString("c");
txtNetPay.Text = netPay.ToString("c");
}
private void btnClear_Click(object sender, EventArgs e)
{
hourlyRate = 0;
hoursWorked = 0;
grossPay = 0m;
incomeTax = 0m;
netPay = 0m;
txtHourlyRate.Text = "";
txtHoursWorked.Text = "";
txtGrossPay.Text = "";
txtIncomeTax.Text = "";
txtNetPay.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
616.125 undergoes midpoint rounding. decimal uses banker's rounding by default. So (and this is something you can check yourself very easily if you just step through your code):
Math.Round(616.125M, 2) // 616.12M
Of course, you're also rounding to three decimal places, so you're actually getting 616.125M instead of 616.12M (or 616.13M) anyway.
You're doing something quite dangerous - you're guessing around tax calculations. Don't do that. Find the applicable tax laws - they will have the exact method used for calculating taxes. Follow those to a T. They specify where and when and how you should round anything. Most likely, all rounding (except for the final price/tax) is supposed to be done to four decimal places, not two, but again, don't guess - read the laws, and make sure you understand them perfectly.
decimal grossPay = Math.Round(hourlyRate * hoursWorked, 3);
returns 616.125, for which the rest of the calculations correctly lead to 462.09.....
It should be
decimal grossPay = Math.Round(hourlyRate * hoursWorked, 2, MidpointRounding.AwayFromZero);
Note the second parameter, the decimal place to round to, is 2 instead of 3, and the MidpointRounding parameter which will round 616.125 to 616.13 and get the result you expect.
Related
I've been getting this error whenever I run the application.
System.FormatException: 'Input string was not in a correct format.' on the decimal weight = Convert.ToDecimal(txtboxWeight.Text);
I don't know where I went wrong in this part this code is running from my different program.
private void ShowtotalPayment()
{
decimal weight = Convert.ToDecimal(txtboxWeight.Text);
decimal price = Convert.ToDecimal(lblPrice.Text);
int quantity = Convert.ToInt32(NumQuantity.Text);
decimal totalPayment = weight * price * quantity;
lblTotalPayment.Text = totalPayment.ToString();
};
For example, change:
decimal weight = Convert.ToDecimal(txtboxWeight.Text);
To:
decimal weight;
if (Decimal.TryParse(txtboxWeight.Text, out weight)) {
// ... do something with "weight" or proceed in here ...
}
else {
MessageBox.Show("Invalid Decimal in Weight!");
}
The problem of my code is if I put value in pay money(textbox) and I turned to zero giving me an error
System.FormatException: 'Input string was not in a correct format.'
This is my code :
private void txtPM_TextChanged(object sender, EventArgs e)
{
string fee = lblFee.Text.Trim();
string pm = txtPM.Text.Trim();
int number = Convert.ToInt32(fee);
int number2 = Convert.ToInt32(pm);
int minus = number2 - number;
txtChange.Text = minus.ToString().Trim();
}
I will put here the form
I hope you can help me thanks guys
You may need Convert.ToDecimal() if you are working with monetary values specified as decimals. (I second the empty and null checking suggestions)
Update for clarification:
Currently, you are converting string to Int32 with the following:
int number = Convert.ToInt32(fee);
int number2 = Convert.ToInt32(pm);
You can instead convert to decimal with the following:
decimal number = Convert.ToDecimal(fee);
decimal number2 = Convert.ToDecimal(pm);
Update2 (full method updated with null and empty checks):
private void txtPM_TextChanged(object sender, EventArgs e)
{
string fee = lblFee.Text.Trim();
string pm = txtPM.Text.Trim();
decimal number = 0;
decimal number2 = 0;
if(!string.IsNullOrWhiteSpace(fee)) number = Convert.ToDecimal(fee);
if(!string.IsNullOrWhiteSpace(pm)) number2 = Convert.ToDecimal(pm);
decimal minus = number2 - number;
txtChange.Text = minus.ToString().Trim();
}
Use the decimal type because it's more appropriate for financial and monetary calculations. To avoid the exception use decimal.TryParse. If the strings pm and fee are not in the right format decimal.TryParse will return zero and no exception will be thrown. You can also remove the Trim() on the last line.
private void txtPM_TextChanged(object sender, EventArgs e)
{
string fee = lblFee.Text.Trim();
string pm = txtPM.Text.Trim();
decimal number;
decimal.TryParse(fee, out number);
decimal number2;
decimal.TryParse(pm, out number2);
decimal minus = number2 - number;
txtChange.Text = minus.ToString();
}
You can use TryParse as already pointed out in the duplicate.
Also it makes more sense to use decimal when working with monetary values.
private void txtPM_TextChanged(object sender, EventArgs e)
{
decimal.TryParse(lblFee.Text, out decimal number);
decimal.TryParse(txtPM.Text, out decimal number2);
txtChange.Text = (number2 - number).ToString();
}
When using C# 6.0 or earlier you have to declare the variables in a separate statement:
private void txtPM_TextChanged(object sender, EventArgs e)
{
decimal number;
decimal.TryParse(lblFee.Text, out number);
decimal number2;
decimal.TryParse(txtPM.Text, out number2);
txtChange.Text = (number2 - number).ToString();
}
private void txtFinal_Leave_1(object sender, EventArgs e)
{
int prelim;
int midterm;
int final;
decimal average;
string remarks;
prelim = int.Parse(txtPrelim.Text);
midterm = int.Parse(txtMidterm.Text);
final = int.Parse(txtFinal.Text);
average = (prelim + midterm + final) / 3;
txtAverage.Text = average.ToString();
if (average >= 75)
{
remarks = "passed";
}
else
{
remarks = "failed";
}
txtRemarks.Text = remarks;
// this is the output 83 passed
// I want to be like this 83.25 passed
}
average = (prelim + midterm + final) / 3.0m;
This will fix your problem.
Int is an integer type; dividing two ints performs an integer division, i.e. the fractional part is truncated since it can't be stored in the result type (also int!). Decimal, by contrast, has got a fractional part. By invoking Decimal.Divide, your int arguments get implicitly converted to Decimals.
You can enforce non-integer division on int arguments by explicitly casting at least one of the arguments to a floating-point type, e.g.: 3.0m this is casting to decimal !
please upgrade your code as follow:
average = Convert.ToDecimal(prelim + midterm + final) / 3;
txtAverage.Text = string.Format("{0:0.00}", average);
I am not sure if I am doing it right, but I am trying to get the CalculateGrossPay method to run when I click the CalculateBttn_Click button during run time. However, I get random syntax errors that constantly change for whatever reason. I do not change any to make that happen. I am supposed to make a separate method to calculate the gross pay.
private void CalculateBttn_Click(object sender, EventArgs e)
{ CalculateGrossPay(decimal hours, decimal rate);
}
private decimal CalculateGrossPay(decimal hours, decimal rate)
{
decimal result=0.00m;
decimal standardHours= 0.00m;
decimal overtimeHours=0.00m;
if (hours > 40)
{
overtimeHours = (hours - 40) * ( (rate) * 1.5m);
standardHours = 40 * rate;
DisplayOutPut.Text = name+ NameTextBox.Text + "";
DisplayOutPut.Text = "Gross Pay:" + result;
}
else
{
standardHours = hours * rate;
DisplayOutPut.Text = "Hours:" + HoursTextBox.Text;
DisplayOutPut.Text = "Rate:" + RateTextBox.Text;
}
result = standardHours + overtimeHours;
return result;
}
Methods in C# that specify parameters need to be called with Parameters. CalculateGrossPay defines 2 Parameters the hours and the rate as decimal values.
Therefore you need to call CalculateGrossPay with two decimal values as Parameters (see example).
Please visit the following sites for more information:
Methods MSDN
Variables MSDN
Example:
CalculateGrossPay(10.1,0.4);
The error is in the way you are calling the method. You should the call to your method to something like this:
decimal hours = 1;
decimal rate = 5;
CalculateGrossPay(hours, rate);
The values of hours and rate can be hardcoded or you can read them from the user interface.
It's a little more complicated than that, but this is my code:
private void button1_Click(object sender, EventArgs e)
{
Decimal num1 = Convert.ToDecimal(textBox1.Text);
Decimal num2 = Convert.ToDecimal(textBox2.Text);
Decimal total = num1 + num2;
textBox3.Text = total.ToString("C");
Decimal total2 = Convert.ToDecimal(total);
total2 = total * 4.2;
textBox4.Text = Convert.ToString(total2);
Basically it's this: I have 4 text boxes, and I want to be able to put in a number in box1 and box2. Box three will multiply box 1 & 2 and convert it to currency. Box four will take Box 3s value and change it back to decimal and multiply a number. I can get it to work as long as total2 does not have a decimal. When it has one it will fail.
The program is basically a cash register program that you put in the following:
QTY (box 1)
Amonunt (box 2)
Subtotal (box 3)
Total (box 4)
Any ideas will be helpful.
Thanks,
Caleb
If I understand the problem correctly you perform operations on decimal variables holding currency values. Often it means maintaining a specific resolution (e.g 2 digits after decimal point for cents). Since Decimal is a general purpose type you need to maintain the required resolution programmatically.
example:
static Decimal RoundToCents(Decimal value)
{
return Math.Round(value, 2, MidpointRounding.AwayFromZero);
}
Decimal num1 = RoundToCents(Convert.ToDecimal(textBox1.Text));
Decimal num2 = RoundToCents(Convert.ToDecimal(textBox2.Text));
Decimal total = num1 + num2; // no rounding is needed for additions and subtractions
Decimal total2 = RoundToCents(total * 4.2m);
The fundamental problem is that you are mixing decimal and double in this line:
total2 = total * 4.2;
total is decimal and 4.2 is a double literal. To write a decimal literal use the m suffix.
total2 = total * 4.2m;