【问题标题】:report an amount in decimal places以小数位报告金额
【发布时间】:2021-01-26 16:43:49
【问题描述】:

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

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

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

例如:

预期值> 单次申报: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) + ")";
            }
            
        }

【问题讨论】:

  • 一般提示:切勿使用floatdouble 等原始数据类型进行货币计算。这些数据类型的精度有限,因此不能代表所有十进制数字,因此从长远来看会导致计算错误。请参阅:Is floating point math broken? - 如果该代码应该对人们的真钱进行计算,我强烈建议使用像 BigDecimal 这样的非原始数据类型。

标签: java database report decimalformat tax


【解决方案1】:

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

【讨论】:

  • 啊,是的,我在法律上是盲人
【解决方案2】:

// 单身状态 如果(状态 == 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) + ")";
        }
        
    }

【讨论】:

    【解决方案3】:

    您正在将类型转换为 int,但您希望结果是双倍的。从代码中删除“(int)”。你可以这样修改代码,

    double firstTax = (int)(income * (0.10)) => double firstTax = (income * (0.10))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 2020-10-25
      • 2014-01-14
      • 2022-07-12
      相关资源
      最近更新 更多