首页 > 解决方案 > 以小数位报告金额

问题描述

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

税收是根据以下方案计算的:

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

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

例如:

预期价值> 单次申请:42806 美元。5 0

实际价值> 单次申报:42806 美元。0 0

到目前为止,这是我的代码:

// 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) + ")";
            }
            
        }

标签: javadatabasereportdecimalformattax

解决方案


您正在将它们投射到int. 这将摆脱任何小数。删除(int)它,它应该有正确的小数。


推荐阅读