当前位置: 首页 > news >正文

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

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)} &lt;<i>op</i>&gt; {@code 0)}, where* &lt;<i>op</i>&gt; 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 --> falseboolean b1 = a.compareTo(b) == 1;System.err.println("b1:" + b1);if (a.compareTo(b) == 1)System.out.println("a > b");// a<b --> trueboolean b2 = a.compareTo(b) == -1;System.err.println("b2:" + b2);if (a.compareTo(b) == -1)System.out.println("a < b");/**** 大于等于*/// a>=b --> falseboolean b3 = a.compareTo(b) > -1;System.err.println("b3:" + b3);if (a.compareTo(b) != -1)System.out.println("a >= b");// a>=c --> trueboolean b4 = a.compareTo(c) > -1;System.err.println("b4:" + b4);/**** 小于等于*/// a<=b --> trueboolean b5 = a.compareTo(b) < 1;System.err.println("b5:" + b5);if (a.compareTo(b) != 1)System.out.println("a <= b");// a<=c --> trueboolean b6 = a.compareTo(c) < 1;System.err.println("b6:" + b6);/**** 等于*/// a=c --> 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);
http://www.lryc.cn/news/264260.html

相关文章:

  • 开源键盘工程QMK
  • Elasticsearch的批量bulk 提交 写入的方式会有顺序问题吗?
  • 云原生之深入解析如何使用Vcluster Kubernetes加速开发效率
  • PCL 已知同名点对计算旋转矩阵并对点云进行旋转
  • MyBatis ORM映射
  • 在线客服系统推荐:为何选择Zoho Desk?
  • 手绘心情树叶,探索情绪世界
  • 智能优化算法应用:基于水基湍流算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • 打开和关闭GBASE南大通用数据库连接
  • Zookeeper 集群搭建过程中常见错误
  • Linux开发工具——vim篇
  • 基于YOLOv5的吸烟检测系统设计与实现
  • 递归算法:二叉树前序、中序、后序遍历解析与递归思想深度剖析
  • WebGL开发数字孪生项目
  • 【51单片机系列】C51中的中断系统扩展实验
  • Poi实现复杂Excel导出,理解POI操作Excel思路!!!
  • 关于 jsconfig.json 文件在导入文件路径提示方面
  • 验证码:防范官网恶意爬虫攻击,保障用户隐私安全
  • python学习笔记--异常捕获
  • ChatGPT如何计算token数?
  • 页面菜单,通过get请求一个url后,跳转另外一个页面,+丢失问题
  • 高并发场景下的延时双删
  • log4js-node在nodejs项目中的使用示例
  • Java_集合进阶(Collection和List系列)
  • QT GUI代码大全(MainWindow, QFile, QPainter, QGraphicsItem/Scene/View)
  • C# Onnx Yolov8 Detect 物体检测 多张图片同时推理
  • 学习使用js保留两位小数同时去掉小数末尾多余的00
  • linux驱动的学习 驱动开发初识
  • Node.js中npm中ws的WebSocket协议的实现
  • PHP HTTPoxy CGI 应用程序漏洞 CVE-2016-5385