品牌商城网站项目,六安人论坛最新招聘,零成本游戏网站开发,定做衣服的店附近哪里有jwensh2023.12.20 使用背景
对 web3 相关的数据进行计算的时候#xff0c;需要进行大小加减计算#xff0c;UI 自动化过程需要将数据转为自然数#xff1b;页面获取的数据会有千分位 、高精度(18位) /*** Compares this {code BigDecimal} with the specified* {code BigDe… jwensh2023.12.20 使用背景
对 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)} lt;iop/igt; {code 0)}, where* lt;iop/igt; 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;}比较方法
与开发讨论过后端业务中比较常用的是 BigDecimalBigDecimal 类提供 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);/**** 大于 和 小于*/// ab -- falseboolean b1 a.compareTo(b) 1;System.err.println(b1: b1);if (a.compareTo(b) 1)System.out.println(a b);// ab -- trueboolean b2 a.compareTo(b) -1;System.err.println(b2: b2);if (a.compareTo(b) -1)System.out.println(a b);/**** 大于等于*/// ab -- falseboolean b3 a.compareTo(b) -1;System.err.println(b3: b3);if (a.compareTo(b) ! -1)System.out.println(a b);// ac -- trueboolean b4 a.compareTo(c) -1;System.err.println(b4: b4);/**** 小于等于*/// ab -- trueboolean b5 a.compareTo(b) 1;System.err.println(b5: b5);if (a.compareTo(b) ! 1)System.out.println(a b);// ac -- trueboolean b6 a.compareTo(c) 1;System.err.println(b6: b6);/**** 等于*/// ac -- trueboolean 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);