当前位置:网站首页>Compareto() and equals() methods of BigDecimal class
Compareto() and equals() methods of BigDecimal class
2022-06-28 10:57:00 【Hello_ xzy_ Word】
BigDecimal Class compareTo() and equals() Method
1. compareTo() Source code
/** * 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 Annotation translation
Will the current BigDecimal Instance and given BigDecimal Example comparison . This method holds that , Same value but different precision ( for example :2.0 and 2.00) Of the two BigDecimal It's the same .
The total return value of this method is 3 Kind of :
- -1 : At present BigDecimal Is less than the given BigDecimal.
- 0 : At present BigDecimal The value of is equal to the given BigDecimal.
- 1 : At present BigDecimal Is greater than the given BigDecimal.
1.2 Implementation details
2. equals() Source code
/** * 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 Annotation translation
Will the current BigDecimal Instance and given BigDecimal Example comparison . This method holds that , Only two with the same value and precision BigDecimal Is the same ( Therefore, this method considers 2.0 and 2.00 Is different ).
The total return value of this method is 2 Kind of :
- true : At present BigDecimal The value of is equal to the given BigDecimal.
- false: At present BigDecimal The value of is not equal to the given BigDecimal.
2.2 Implementation details
3. summary
BigDecimal Class compartTo Methods and equals Methods can be used to compare BigDecimal The size of the value represented by the instance . However, there are some differences between the two methods in judging whether the two values are equal :compartTo Only two numbers are required (value) equal ,equals The value and precision of two numbers are required (scale) All must be equal .
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
边栏推荐
猜你喜欢
随机推荐
sentinel
Makefile简介
MySQL查看数据库性能常用命令
拼接String集合中的字符串_基于Stream
sentinel
将浏览器中的文件 url转换为File流
Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
Using loops for, while, and if in katalon else、break、continue
[monkey] Introduction to monkey test
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
appliedzkp zkevm(10)中的Transactions Proof
ICMP协议的作用,Ping of Death攻击的原理是什么?
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
datetime与logging模块
Wealth management for programmers
JS foundation 1-js introduction and operator
Katalon框架测试一个web页面操作实例代码
【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)
[QT] connect syntax reference implementation









