c# 使用运算符进行计算(净工资)(总工资)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/652928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# using operators with calculations (net pay) (gross pay)
提问by DEV_Beginner
I don't understand if the calculation for netPay = grossPay - (fedtax withholding + social security tax withholding)
. Are my calculations correct within the program? Dealing with such in editedTax
?? If someone could help I'd appreciate it.
我不明白netPay = grossPay - (fedtax withholding + social security tax withholding)
. 我的计算在程序中是否正确?处理这样的editedTax
??如果有人可以提供帮助,我将不胜感激。
More info: When I display netPay
within an output, I receive a runtime error, where I couldn't convert the negative to currency with {0:c2}
.
更多信息:当我netPay
在输出中显示时,我收到一个运行时错误,我无法将负数转换为货币{0:c2}
。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
string empName;
string userInput;
double netPay;
double editedTax1;
double grossPay;
double editedTax2;
double hrsWorked;
double ovtWorked;
double payRate;
const double FED_TAX = .28;
const double SS_TAX = 7.65;
// step 1
Console.WriteLine(" WEEKLY PAYROLL INFORMATION");
// step 2
Console.WriteLine(" --------------------------");
// step 3
Console.Write("\n Please enter the employer's name: ");
empName = Console.ReadLine();
//step 4
Console.Write("\n Please enter the number of hours worked this week: ");
userInput = Console.ReadLine();
hrsWorked = Convert.ToDouble(userInput);
// step 5
Console.Write("\n Please enter the number of OVERTIME HOURS worked this week: ");
userInput = Console.ReadLine();
ovtWorked = Convert.ToInt32(userInput);
// step 6
Console.Write("\n Please enter employee's HOURLY PAY RATE: ");
userInput = Console.ReadLine();
payRate = Convert.ToDouble(userInput);
// step 7
grossPay = (hrsWorked * payRate + ovtWorked * 1.5 * payRate);
// step 8
editedTax1 = FED_TAX * grossPay;
// step 9
editedTax2 = SS_TAX * grossPay;
// step 10
netPay = editedTax1 + editedTax2 - grossPay;
// step 11
Console.WriteLine("\n\n The weekly payroll information summary for: " + empName);
Console.WriteLine("\n Gross pay: {0:C2} ", grossPay);
// step 12
Console.WriteLine(" Federal income taxes witheld: {0:C2} ", editedTax1);
Console.WriteLine(" Social Security taxes witheld: {0:C2} ", editedTax2);
Console.WriteLine(" Net Pay: {0:C2}", netPay);
}
}
}
回答by recursive
netPay
is assigned the opposite value in the code as compared to your description below.
I don't see any syntax errors or anything.
netPay
与您在下面的描述相比,在代码中分配了相反的值。
我没有看到任何语法错误或任何东西。
What is the problem you're having? What are some of the things you've tried?
你有什么问题?您尝试过哪些方法?
回答by lc.
I'm not sure exactly what you're asking for, but simple substitution in the assignment statements yields the following formulas:
我不确定您到底要什么,但赋值语句中的简单替换会产生以下公式:
Since
自从
editedTax1 = FED_TAX * grossPay;
editedTax2 = SS_TAX * grossPay;
netPay = editedTax1 + editedTax2 - grossPay;
then
然后
netPay = FED_TAX * grossPay + SS_TAX * grossPay - grossPay;
meaning
意义
netPay = grossPay * (FED_TAX + SS_TAX - 1);
so something seems a little off here...
所以这里似乎有点不对劲……
Are you sure you don't want
你确定你不想
netPay = grossPay - (editedTax1 + editedTax2);
instead of
代替
netPay = editedTax1 + editedTax2 - grossPay;
This seems to match what you're looking for as
这似乎与您正在寻找的内容相匹配
netPay = grossPay - (FED_TAX * grossPay + SS_TAX * grossPay);
or
或者
netPay = grossPay * (1 - (FED_TAX + SS_TAX));
...unless I'm missing something, of course.
...当然,除非我遗漏了什么。
Edit: I was missing something. Your tax constants are a percent, but you're not dividing by 100 when you do calculations with them. You have two options:
编辑:我错过了一些东西。你的税收常数是一个百分比,但当你用它们进行计算时,你并没有除以 100。您有两个选择:
Divide by 100 when you use the values in a calculation, like:
editedTax1 = (FED_TAX / 100) * grossPay;
Store the constants as the decimal representation, not a percent, like:
const double FED_TAX = .0028;
当您在计算中使用这些值时,除以 100,例如:
editedTax1 = (FED_TAX / 100) * grossPay;
将常量存储为十进制表示,而不是百分比,例如:
const double FED_TAX = .0028;
回答by Jim Mischel
The reason you're getting a negative number in the calculation is because your SS_TAX is 7.65. I think the number you want is 0.0765.
您在计算中得到负数的原因是您的 SS_TAX 为 7.65。我想你想要的数字是 0.0765。
回答by John Rasch
I have tested this code on my machine and it works correctly:
我已经在我的机器上测试了这段代码,它工作正常:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
const double FEDERAL_TAX_RATE= 0.28;
const double SOCIAL_SECURITY_RATE = 0.0765; // I am assuming the 7.65 was supposed to be 7.65%... therefore it should be 0.0765
Console.WriteLine(" WEEKLY PAYROLL INFORMATION");
Console.WriteLine(" --------------------------");
Console.Write("\n Please enter the employer's name: ");
string empName = Console.ReadLine();
Console.Write("\n Please enter the number of hours worked this week: ");
double hrsWorked = Convert.ToDouble(Console.ReadLine());
Console.Write("\n Please enter the number of OVERTIME HOURS worked this week: ");
double ovtWorked = Convert.ToInt32(Console.ReadLine());
Console.Write("\n Please enter employee's HOURLY PAY RATE: ");
double payRate = Convert.ToDouble(Console.ReadLine());
double grossPay = CalculateGrossPay(hrsWorked, payRate, ovtWorked);
double federalTaxWithheld = CalculateTax(grossPay, FEDERAL_TAX_RATE);
double socialSecurityWithheld = CalculateTax(grossPay, SOCIAL_SECURITY_RATE);
double netPay = CalculateNetPay(grossPay, federalTaxWithheld + socialSecurityWithheld);
Console.WriteLine("\n\n The weekly payroll information summary for: " + empName);
Console.WriteLine("\n Gross pay: {0:C2} ", grossPay);
Console.WriteLine(" Federal income taxes witheld: {0:C2} ", federalTaxWithheld);
Console.WriteLine(" Social Security taxes witheld: {0:C2} ", socialSecurityWithheld);
Console.WriteLine(" Net Pay: {0:C2}", netPay);
Console.ReadLine(); // You don't need this line if your running from the command line to begin with
}
static double CalculateGrossPay(double HoursWorked, double PayRate, double OvertimeHoursWorked)
{
return PayRate * (HoursWorked + 1.5 * OvertimeHoursWorked);
}
static double CalculateTax(double GrossPay, double TaxRate)
{
return GrossPay * TaxRate;
}
static double CalculateNetPay(double GrossPay, double TaxAmount)
{
return GrossPay - TaxAmount;
}
}
}
Since it looks as though this is your first programming course, I'll offer you some pointers that they probably will not emphasize in your class:
由于这看起来好像是您的第一门编程课程,因此我将向您提供一些他们可能不会在您的课堂上强调的提示:
- Use descriptive variable names. If you notice yourself putting a number after your variable name, it can probably done a different and more readable way!
- Utilize functions, they bundle common tasks in one section of code and increase readability significantly.
- You may want to add some exception handlingor validation to this code. For example, what if you accidentally passed
-1
intoOverTimeHours
?
- 使用描述性变量名称。如果您注意到自己在变量名后放了一个数字,那么它可能会以不同的方式完成并且更具可读性!
- 利用函数,它们将常见任务捆绑在一段代码中并显着提高可读性。
- 您可能希望向此代码添加一些异常处理或验证。例如,如果您不小心传入
-1
了OverTimeHours
怎么办?
I understand this might not matter at this point in your programming journey, but it's always good to start off using coding techniques that make your code more readable and less confusing, especially for people who may have to maintain it in the future.
我知道这在您的编程旅程中此时可能并不重要,但是开始使用使您的代码更具可读性和更少混乱的编码技术总是好的,尤其是对于将来可能需要维护它的人。