【问题标题】:BigDecimal setScale method syntax not workingBigDecimal setScale 方法语法不起作用
【发布时间】:2018-07-04 11:34:35
【问题描述】:

这段代码在我提到 setScale(); 的那一行给了我一个错误。 它给了我一个错误,说需要四舍五入。我正在尝试制作一个应用 cp 和 sp 然后根据盈亏找到盈亏百分比。有人可以帮我解决 BigDecimal 语句吗?

```java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

abstract class functions
{
    abstract void cpSP();
}

public class Percentage
{
    static Scanner sc = new Scanner(System.in);
    public void cpSP()
    {
        System.out.println("Enter the Cost Price:");
        double CP = sc.nextDouble();
        System.out.println("Enter the Selling Price:");
        double SP = sc.nextDouble();
        if (CP > SP)
        {
            System.out.println("As the Cost Price is greater than the Selling Price, there will be LOSS.");
            System.out.println("Loss = CP - SP");
            double loss = CP - SP;
            System.out.println("Loss = " + loss);
            System.out.println("Now,");
            System.out.println("Loss percentage = Loss/CP x 100");
            System.out.println("");
            double lossPercentage = (loss/CP)*100;
            String lPString = Double.toString(lossPercentage);
            BigDecimal bd = new BigDecimal(lPString);
            bd.setScale(2);
            System.out.println("Loss percentage = " + bd + "%" );
        }
        else if (SP > CP) 
        {
            System.out.println("As the Cost Price is less than the Selling Price, there will be PROFIT.");
            System.out.println("Profit = SP - CP");
            double profit = SP - CP;
            System.out.println("Profit = " + profit);
            System.out.println("Now,");
            System.out.println("Profit percentage = Profit/CP x 100");
            double profitPercentage = (profit / CP)*100;
            String pPString = Double.toString(profitPercentage);
            BigDecimal bd = new BigDecimal(pPString).setScale(2, RoundingMode.UNNECESSARY);
            System.out.println("Proft percentage = " + bd + "%" );
        }
        else
        {
            System.out.println("No profit, no loss.");
        }
    }


    public static void main(String[] args)
    {
        Percentage p = new Percentage();
        System.out.println("Select an option:");
        System.out.println("1. Find profit/loss percentage where the CP and SP is given.");
        int select = sc.nextInt();

        if (select == 1)
        {
           p.cpSP(); 
        }
    }

}
```

错误是:

Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
    at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148)
    at java.math.BigDecimal.needIncrement(BigDecimal.java:4204)
    at java.math.BigDecimal.divideAndRound(BigDecimal.java:4112)
    at java.math.BigDecimal.setScale(BigDecimal.java:2452)
    at java.math.BigDecimal.setScale(BigDecimal.java:2386)
    at maths.Percentage.cpSP(Percentage.java:45)
    at maths.Percentage.main(Percentage.java:64)

【问题讨论】:

  • 你能发布确切的错误吗
  • 线程“主”java.lang.ArithmeticException 中的异常:在 java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148) 处需要舍入 java.math.BigDecimal.needIncrement(BigDecimal.java: 4204) 在 java.math.BigDecimal.divideAndRound(BigDecimal.java:4112) 在 java.math.BigDecimal.setScale(BigDecimal.java:2452) 在 java.math.BigDecimal.setScale(BigDecimal.java:2386) 在数学。 Math.Percentage.main(Percentage.java:64) 处的 Percentage.cpSP(Percentage.java:45)

标签: java percentage bigdecimal


【解决方案1】:

setScale方法的javadoc中有明确提到

@throws ArithmeticException 如果 {@code roundingMode==ROUND_UNNECESSARY} 并且指定的缩放操作需要舍入。

示例: 这会抛出上述异常

new BigDecimal("1200.1234").setScale(2, BigDecimal.ROUND_UNNECESSARY)

因为当您将其比例减小到 2 时,您需要指定如何舍入 - 向上舍入 (1200.13) 或向下舍入 (1200.12)

如果您只需要两位小数,请选择一种舍入模式(以您可以接受的为准)

【讨论】:

  • 那么我应该怎么做才能将百分比保留到小数点后 2 位?
  • 只需选择一种舍入模式(以您可以接受的为准)
  • 非常感谢,它现在正在工作。我在使用文档时遇到了一些问题
  • 这是正确的答案,但我建议您避免使用双重原因 stackoverflow.com/questions/322749/…。阅读 sc.nextBigDecimal 并使用 BigDecimal 执行算术运算
猜你喜欢
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 2016-11-29
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多