当前位置:网站首页>BigDecimal 类的 compareTo() 和 equals()方法
BigDecimal 类的 compareTo() 和 equals()方法
2022-06-28 10:52:00 【Hello_xzy_Word】
BigDecimal 类的 compareTo() 和 equals()方法
1. compareTo()源码
/** * Compares this BigDecimal with the specified BigDecimal. * Two 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(<,<=,==,>,>=,!=). The suggested idiom for * performing these comparisons is: x.compartTo(y) <op> 0, * where <op> is one of the six comparison operators. * * @param val - BigDecimal to which this BigDecimal is to be * compared. * @return -1,0 or 1 as this BigDecimal is numerically less than, * equal to, or greater than 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;
}
1.1 注释翻译
将当前 BigDecimal 实例与给定 BigDecimal 实例进行比较。本方法认为,数值相同但精度不同(例如:2.0 和 2.00)的两个 BigDecimal 是相同的。
本方法的返回值总共有3种:
- -1 :当前 BigDecimal 的数值小于给定的 BigDecimal。
- 0 :当前 BigDecimal 的数值等于给定的 BigDecimal。
- 1 :当前 BigDecimal 的数值大于给定的 BigDecimal。
1.2 实现细节
2. equals()源码
/** * Compares this BigDecimal with the specified Object for equality. * Unlike compareTo, thi method considers two BigDecimal objects equal * only if they are equal in value and scale (thus 2.0 is not equal to 2.00 * when compared by this method). * * @param x - Object to which this BigDecimal is to to compared. * @return - true if and only if the specified Object is a BigDecimal whose * value and scale are equal to this BigDecimal`s. */
@Override
public boolean equals(Object x) {
if (!(x instanceof BigDecimal))
return false;
BigDecimal xDec = (BigDecimal) x;
if (x == this)
return true;
if (scale != xDec.scale)
return false;
long s = this.intCompact;
long xs = xDec.intCompact;
if (s != INFLATED) {
if (xs == INFLATED)
xs = compactValFor(xDec.intVal);
return xs == s;
} else if (xs != INFLATED)
return xs == compactValFor(this.intVal);
return this.inflated().equals(xDec.inflated());
}
2.1 注释翻译
将当前 BigDecimal 实例与给定 BigDecimal 实例进行比较。本方法认为,只有数值和精度都相同的两个 BigDecimal 才是相同的(因此本方法认为2.0 和 2.00 是不同的)。
本方法的返回值总共有2种:
- true :当前 BigDecimal 的数值等于给定的 BigDecimal。
- false:当前 BigDecimal 的数值不等于给定的 BigDecimal。
2.2 实现细节
3. 总结
BigDecimal 类的 compartTo 方法和 equals 方法都能用于比较 BigDecimal 实例代表的数值的大小。但两个方法在判别两个数值是否相等的标准上存在一定的差异:compartTo 仅要求两个数的值(value)相等,equals 要求两个数的值和精度(scale)都要相等。
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("2.0");
BigDecimal b2 = new BigDecimal("2.00");
System.out.println("b1.compareTo(b2):" + (b1.compareTo(b2) == 0));
System.out.println("b1.equals(b2):" + b1.equals(b2));
}
b1.compareTo(b2):true
b1.equals(b2):false
Process finished with exit code 0
边栏推荐
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- Hystrix 部署
- 使用 Calendar 计算时间
- Mongo database
- 无线通信模块定点传输-点对多点的具体传输应用
- Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
- 【功能建议】多个工作空间启动时选择某个空间
- [Unity]EBUSY: resource busy or locked
- 物联网无线通信应用中6种融合定位技术
- 方法重写(Override)
猜你喜欢
![[practice] appium settings app is not running after 5000ms](/img/3c/5a7544561231a15a4bb5cfa9e102b3.png)
[practice] appium settings app is not running after 5000ms

一种跳板机的实现思路

MySQL cannot be opened. Flash back

Ruoyi integrated building block report (NICE)
![[unity] built in rendering pipeline to URP](/img/a5/3ae37b847042ffb34e436720f61d17.png)
[unity] built in rendering pipeline to URP

Idea failed to connect to SQL Sever

How does ETF position affect spot gold price?

MySQL general binary installation method

ruoyi集成积木报表(nice)

Yann LeCun新论文:构建自动智能体之路
随机推荐
Ribbon core source code analysis
Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
AQS understanding
Several methods of using ABAP to operate Excel
物联网5种无线传输协议特点大汇总
[cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
Fastposter v2.8.4 release e-commerce poster generator
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)
建立自己的网站(11)
【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)
Katalon当中的debug调试
使用 Calendar 计算时间
Redis database
Hystrix deployment
Metersphere uses JS to refresh the current page
Pop up and push in sequence of stack < difficulty coefficient >
File的io流与base64
etf持仓如何影响现货金价?
【agora】get 一个 agora_refptr 对象的用法示例
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison