以小数位报告金额

问题描述

我正在制定所得税计划,其中的计算基于 - 申报状态和 - 应税收入

 The tax is calculated based on the following scheme:

所有部分必须在小数点后有两位数。

问题是输出在小数位后显示两个零,而不是预期的数字。

例如:

预期价值> 单次申请: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))

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...