如何解决以小数位报告金额
我正在制定所得税计划,其中的计算基于 - 申报状态和 - 应税收入
所有部分必须在小数点后有两位数。
问题是输出在小数位后显示两个零,而不是预期的数字。
例如:
预期价值> 单次申请: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",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",secondTax) + ",Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}
解决方法
您正在将它们投射到 int
。这将摆脱任何小数。
删除 (int)
,它应该有正确的小数。
// 单身状态 如果(状态== 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",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",secondTax) + ",Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}
,
您正在将类型转换为 int,但您希望结果翻倍。从代码中删除“(int)”。您可以像这样更改代码,
double firstTax = (int)(income * (0.10)) => double firstTax = (income * (0.10))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。