【Java】BigDecimal 比较自动化页面获取数据的大小

[email protected]

使用背景

对 web3 相关的数据进行计算的时候,需要进行大小加减计算,UI 自动化过程需要将数据转为自然数;页面获取的数据会有千分位高精度(18位)

    /**
     * Compares this {@code BigDecimal} with the specified
     * {@code BigDecimal}.  Two {@code BigDecimal} objects that are
     * equal in value but have a different scale (like 2.0 and 2.00)
     * are considered equal by this method.  This method is provided
     * in preference to individual methods for each of the six boolean
     * comparison operators ({@literal <}, ==,
     * {@literal >}, {@literal >=}, !=, {@literal <=}).  The
     * suggested idiom for performing these comparisons is:
     * {@code (x.compareTo(y)} <op> {@code 0)}, where
     * <op> is one of the six comparison operators.
     *
     * @param  val {@code BigDecimal} to which this {@code BigDecimal} is
     *         to be compared.
     * @return -1, 0, or 1 as this {@code BigDecimal} is numerically
     *          less than, equal to, or greater than {@code val}.
     */
    public int compareTo(BigDecimal val) {
        // Quick path for equal scale and non-inflated case.
        if (scale == val.scale) {
            long xs = intCompact;
            long ys = val.intCompact;
            if (xs != INFLATED && ys != INFLATED)
                return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
        }
        int xsign = this.signum();
        int ysign = val.signum();
        if (xsign != ysign)
            return (xsign > ysign) ? 1 : -1;
        if (xsign == 0)
            return 0;
        int cmp = compareMagnitude(val);
        return (xsign > 0) ? cmp : -cmp;
    }

比较方法

  • 与开发讨论过,后端业务中比较常用的是 BigDecimal
  • BigDecimal 类提供 compareTo() 方法来比较两个数的大小
    • 例如:a = b 返回0 ; a < b返回-1 ; a > b返回1
    • 通过这三种比较返回的结果,我们还可以比较 a != b、a >= b和a<= b这三种情况。
import java.math.BigDecimal;

public class Web3WalletTest {
    public static void main(String[] args) {
		BigDecimal a = new BigDecimal("213.003");
        BigDecimal b = new BigDecimal("213.004");
        BigDecimal c = new BigDecimal("213.003");

        /***
         * 大于 和 小于
         */
        // a>b --> false
        boolean b1 = a.compareTo(b) == 1;
        System.err.println("b1:" + b1);
        if (a.compareTo(b) == 1)
            System.out.println("a > b");
            
        // a true
        boolean b2 = a.compareTo(b) == -1;
        System.err.println("b2:" + b2);
        if (a.compareTo(b) == -1)
            System.out.println("a < b");

        /***
         * 大于等于
         */
        // a>=b --> false
        boolean b3 = a.compareTo(b) > -1;
        System.err.println("b3:" + b3);
        if (a.compareTo(b) != -1)
            System.out.println("a >= b");
        
        // a>=c --> true
        boolean b4 = a.compareTo(c) > -1;
		System.err.println("b4:" + b4);
        
        /***
         * 小于等于
         */
        // a<=b --> true
        boolean b5 = a.compareTo(b) < 1;
        System.err.println("b5:" + b5);
        if (a.compareTo(b) != 1)
            System.out.println("a <= b");
        
        // a<=c --> true
        boolean b6 = a.compareTo(c) < 1;
		System.err.println("b6:" + b6);
        
        /***
         * 等于
         */
        // a=c --> true
        boolean b7 = a.compareTo(c) == 0;
		System.err.println("b7:" + b7);
		if (a.compareTo(b) == 0)
            System.out.println("a = b");
        if (a.compareTo(b) != 0)
            System.out.println("a != b");

    }
}
  • 用例里使用的方式
BigDecimal btteBalance = new  BigDecimal(walletPage.getTokenBalance(3).getText().replace(",", ""));
Assert.assertTrue(btteBalance.compareTo(new BigDecimal("0")) > -1);

你可能感兴趣的:(#,效率/接口.性能.自动化,#,Java,java,自动化,bigdecimal)