jwensh@2023.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)} <<i>op</i>> {@code 0)}, where
* <<i>op</i>> 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()
方法来比较两个数的大小
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<b --> 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);