【问题标题】:Float cannot be dereferenced - using BigDecimal无法取消引用浮点数 - 使用 BigDecimal
【发布时间】:2013-01-03 10:02:16
【问题描述】:

我在尝试编写一个计算贷款利息并以特定小数位显示信息的程序时遇到错误。我需要将loanInterest 显示为3.546%,或者类似的小数点后3 位。我能够让totalInterest 正确显示,但我不知道这是否是因为它是我刚刚建立的新值。当我尝试如下所示运行我的程序时,我收到“无法取消引用浮点数”错误。

public class SarahShelmidineModule2Project {

public static void main(String[] args) {
    //Being programing for Module 2 project

    // Welcome the user to the program
    System.out.println("Welcome to the Interest Calculator");
    System.out.println();  // print a blank line

    // create a Scanner object named sc
    Scanner sc = new Scanner(System.in);

    // perform interest calculations until choice isn't equal to "y" or "Y"
    String choice = "y";
    while (choice.equalsIgnoreCase("y"))
    {
        // get info from user on loan amount and interest rates and store data
        System.out.print("Enter loan amount:   ");
        double loanAmount = sc.nextDouble();
        System.out.print("Enter interest rate:   ");
        float loanInterest = sc.nextFloat();


        // calculate the interest and convert to BigDecimal and rounding for totalInterest

       BigDecimal decimalloanAmount = new BigDecimal (Double.toString(loanAmount));
       BigDecimal decimalloanInterest = new BigDecimal (Double.toString(loanInterest));
       BigDecimal totalInterest = decimalloanAmount.multiply(decimalloanInterest);
       totalInterest = totalInterest.setScale(2, RoundingMode.HALF_UP);

       loanInterest = loanInterest.setScale(3, RoundingMode.HALF_UP);

        System.out.println();  // print a blank line

        // display the loan amount, loan interest rate, and interest
        // also format results to percent and currency
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        NumberFormat percent = NumberFormat.getPercentInstance();

        String message =    "Loan amount: " + currency.format(loanAmount) + "\n"
                +           "Interest rate:  " + percent.format(loanInterest) + "\n"
                +           "Intrest:    " + currency.format(totalInterest) + "\n";
        System.out.println(message);

        // inquire if user would like to continue with application
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();

以下是我运行此程序时遇到的错误:

欢迎使用利息计算器

输入贷款金额:10932

输入利率:.0934

Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous sym type: <any>     at
 sarahshelmidinemodule2project.SarahShelmidineModule2Project.main(SarahShelmidineModule2Project.java:45)

【问题讨论】:

  • 请提供完整的错误信息和堆栈跟踪

标签: java floating-point bigdecimal dereference


【解决方案1】:

改变一下

float loanInterest = sc.nextFloat();

BigDecimal loanInterest = new BigDecimal(sc.nextFloat());

你会解决“float cannot be derefenced”,因为float是一个原始类型并且没有方法setScale

关于打印正确的小数位数,请使用以下内容:

    String currencySymbol = Currency.getInstance(Locale.getDefault()).getSymbol();
    System.out.printf("%s%8.5f\n", currencySymbol, totalInterest);

此代码将使用 5 位小数,但请确保您的 BigDecimal 小数位数至少为 5,否则您将得到不重要的零。

【讨论】:

  • 这解决了这个错误。现在,当程序将数据显示给用户时,我只需要计算百分比即可。谢谢你的那部分。我在那里用于舍入 totalInterest 的代码似乎工作正常。
  • 其实不是。我将其更改为“1”以查看它是否有效,但它没有。所以,我想我现在的问题是我需要找到正确的代码来四舍五入并设置我的分析器中显示的位数。
  • 是的,要逃避百分比,请查看接受的答案:stackoverflow.com/questions/1708444/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多