如何在 Java 中使用 Bigdecimal 数学运算获取值

How to get a value using Bigdecimal mathematical operations in Java

我正在尝试获取产品的税收价值(我作为静态值传递的税收百分比)

使用 Bigdecimal 但出现以下错误是我的代码

        double  dis,amount,s;
        BigDecimal taxprice;




        List<WebElement> firstproductSubtotalprice = getDriver().findElements(By.xpath("//p[@class='m-order-detail-item-name']/span"));
        BigDecimal firstproductSubtotalpricevalue = null;
        for (WebElement webElement : firstproductSubtotalprice) {
            String priceText = webElement.getText();
            if (priceText != null && priceText.length() > 0) {
                firstproductSubtotalpricevalue = new BigDecimal(priceText.replace('$', ' ').trim());
                System.out.println("The First Product Subtotal Price is: "+ firstproductSubtotalpricevalue);
                break;
            }
        }



        List<WebElement> firstproductTaxprice = getDriver().findElements(By.xpath("//span[contains(text(), 'Tax')]/following-sibling::span[1]"));
        BigDecimal firstproductTaxpriceValue = null;
        for (WebElement webElement : firstproductTaxprice) {
            String priceText = webElement.getText();
            if (priceText != null && priceText.length() > 0) {
                firstproductTaxpriceValue = new BigDecimal(priceText.replace('$', ' ').trim());
                System.out.println("The First Product Tax Price is: "+ firstproductTaxpriceValue);
                break;
            }
        }

        taxprice = firstproductSubtotalpricevalue;

        dis=10;

        s=100-dis;

        BigDecimal  c= firstproductTaxpriceValue;


               BigDecimal   a = taxprice.multiply(new BigDecimal(s)); 

               BigDecimal  b =  a.divide(a, 100);



               System.out.println(a+"/"+b+" = "+c);

使用上面的代码我想获取税值并想与第一个productTaxpriceValue进行比较, 如果税额等于 firstproductTaxpriceValue 那么我的测试用例如果没有失败则通过。

请帮我获取税值

除法时不提供舍入模式

下面是方法的声明:

public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)

试试这个:

 BigDecimal  b =  a.divide(new BigDecimal(100),2,RoundingMode.CEILING);

其中 第一个参数 new BigDecimal(100) 是您要除以

的值

第二个参数,2是比例还是否。小数位数

第三个参数RoundingMode.CEILING 是您希望对结果进行舍入的方式。

您在代码末尾执行 BigDecimal b = a.divide(a, 100)100 不是有效的输入。

使用其中之一:

0 (ROUND_UP)
1 (ROUND_DOWN)
2 (ROUND_CEILING)
3 (ROUND_FLOOR)
4 (ROUND_HALF_UP)
5 (ROUND_HALF_DOWN)
6 (ROUND_HALF_EVEN)
7 (ROUND_UNNECESSARY)