以小数位报告金额
report an amount in decimal places
我正在制定一项所得税计划,其中的计算基于 - 申报状态和 - 应税收入
所有部分必须小数点后两位。
问题是输出在小数点后显示两个零,而不是预期的数字。
例如:
预期价值> 单笔申请:42806 美元。50
实际价值> 单笔申报:$42806.00
到目前为止,这是我的代码:
// Single Status
if (status == 1) {
if (income > 0 && income <= 8350) {
double firstTax = (int)(income * (0.10));
double totalTax = firstTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ")";
}
else if (income >= 8350 && income <= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (income - 8350) * (0.15));
double totalTax = firstTax + secondTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ")";
}
else if (income >= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (33950 - 8350) * (0.15));
double thirdTax = (int)((income - 33950) * (0.25));
double totalTax = firstTax + secondTax + thirdTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}
//单身状态
如果(状态== 1){
if (income > 0 && income <= 8350) {
double firstTax = (int)(income * (0.10));
double totalTax = firstTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ")";
}
else if (income >= 8350 && income <= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (income - 8350) * (0.15));
double totalTax = firstTax + secondTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ")";
}
else if (income >= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (33950 - 8350) * (0.15));
double thirdTax = (int)((income - 33950) * (0.25));
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}
您正在将它们投射到 int
。这将消除任何小数。
删除 (int)
,它应该有正确的小数。
您正在将类型转换为 int,但您希望结果为 double。从代码中删除“(int)”。您可以这样更改代码,
double firstTax = (int)(income * (0.10)) => double firstTax = (income * (0.10))
我正在制定一项所得税计划,其中的计算基于 - 申报状态和 - 应税收入
所有部分必须小数点后两位。
问题是输出在小数点后显示两个零,而不是预期的数字。
例如:
预期价值> 单笔申请:42806 美元。50
实际价值> 单笔申报:$42806.00
到目前为止,这是我的代码:
// Single Status
if (status == 1) {
if (income > 0 && income <= 8350) {
double firstTax = (int)(income * (0.10));
double totalTax = firstTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ")";
}
else if (income >= 8350 && income <= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (income - 8350) * (0.15));
double totalTax = firstTax + secondTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ")";
}
else if (income >= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (33950 - 8350) * (0.15));
double thirdTax = (int)((income - 33950) * (0.25));
double totalTax = firstTax + secondTax + thirdTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}
//单身状态 如果(状态== 1){
if (income > 0 && income <= 8350) {
double firstTax = (int)(income * (0.10));
double totalTax = firstTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ")";
}
else if (income >= 8350 && income <= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (income - 8350) * (0.15));
double totalTax = firstTax + secondTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ")";
}
else if (income >= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (33950 - 8350) * (0.15));
double thirdTax = (int)((income - 33950) * (0.25));
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}
您正在将它们投射到 int
。这将消除任何小数。
删除 (int)
,它应该有正确的小数。
您正在将类型转换为 int,但您希望结果为 double。从代码中删除“(int)”。您可以这样更改代码,
double firstTax = (int)(income * (0.10)) => double firstTax = (income * (0.10))