【问题标题】:Java inflation calculator problem not rounding correctly [duplicate]Java通货膨胀计算器问题未正确舍入[重复]
【发布时间】:2021-09-24 15:01:17
【问题描述】:

提示:很难制定跨越数年的预算,因为价格不稳定。 如果您的公司每年需要 200 支铅笔,您不能简单地使用今年的价格 相当于两年后铅笔的成本。由于通货膨胀,成本可能 比今天高。编写一个程序来衡量一个项目的预期成本 指定的年数。程序询问物品的成本、数量 从现在起购买该物品的年份,以及通货膨胀率。这 然后程序在指定期间输出项目的估计成本。 让用户以百分比形式输入通货膨胀率,例如 5.6(百分比)。您的 然后程序应该将百分比转换为分数,例如 0.056,并且应该 使用循环来估计通货膨胀调整后的价格。

代码:

    import java.util.Scanner;
    import java.text.NumberFormat;

    public class InflationCalculator {

        public static void main(String[] args) {
     
           Scanner console = new Scanner(System.in);
   
           System.out.print("Enter price of the Item:");
           double cost = console.nextDouble();
    
           System.out.print("Enter number of years in which it will be purchased:");
           double years = console.nextDouble();
    
           System.out.print("Enter percent of inflation per year:");
           double inflationRate = console.nextDouble();       
     
           inflationRate = inflationRate / 100;
   
           for(int i = 1; i <= years; i++){
           cost += cost * inflationRate;
        }
   
      System.out.println(cost);
     }   
 }

预期结果:

输入·价格·of·the·商品:200↵ 输入·数量·of·年·in·which·it​​·will·be·purchased:50↵ Enter·per·of·inflation·per·year:5↵ 2293.4799571507338

我得到了什么:

输入·价格·of·the·商品:200↵ 输入·数量·of·年·in·which·it​​·will·be·purchased:50↵ Enter·per·of·inflation·per·year:5↵ 2293.479957150734

我不确定为什么它没有正确舍入,我做错了什么???

【问题讨论】:

  • 使用浮点数和双精度时的精度可能是一件棘手的事情。为此,Java 语言建议使用适合这种情况的 BigDecimal 数据类型。查看 javalang 中的 Currency 类。
  • 你必须使用 BigDecimal 而不是 Double。精确计算十进制数是很困难的。
  • 不幸的是,使用 BigDecimal 使情况变得更糟
  • “更糟”是什么意思?向我们展示输出

标签: java calculator


【解决方案1】:

问题:

double 变量类型的精度约为 15-16 decimal points。这就是您的结果被截断的原因。

解决方案:

您可以改用BigDecimalDecimalFormat,如these answers. 中所述

【讨论】:

    【解决方案2】:

    这段代码应该可以正常工作:

        Scanner console = new Scanner(System.in);
    
        System.out.print("Enter price of the Item:");
        BigDecimal cost = new BigDecimal(console.nextDouble());
    
        System.out.print("Enter number of years in which it will be purchased:");
        int years = console.nextInt();
    
        System.out.print("Enter percent of inflation per year:");
        BigDecimal inflationRate = new BigDecimal(console.nextDouble());
    
        inflationRate = inflationRate.divide(new BigDecimal(100));
        
        for(int i = 1; i <= years; i++) {
            cost = cost.add(cost.multiply(inflationRate));
        }
    
        System.out.println(cost);
    

    输出(使用您的输入):2293.4799571507352069702827102422103183593712979003728316020223783073817003241856582462787628173828125000

    如果您想将此数字四舍五入到您的预期结果,您可以使用

    System.out.println(cost.setScale(10, BigDecimal.ROUND_CEILING)); // to round up
    // or
    System.out.println(cost.setScale(10, BigDecimal.ROUND_FLOOR)); // to round down
    

    .setScale() 的第一个参数是你想要显示的十进制数字的个数。 (对不起我的英语不好哈哈)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 2021-08-17
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多