当前位置:网站首页>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
边栏推荐
- 一种跳板机的实现思路
- Using loops for, while, and if in katalon else、break、continue
- 还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
- 数据库系列:有什么办法对数据库的业务表进行无缝升级
- Word、PDF、TXT文件实现全文内容检索需要用什么方法?
- Katalon framework tests web (XX) custom keywords and upload pop-up operations
- Katalon framework tests a web page operation example code
- [cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
- JS基础8
- Spatial-Temporal时间序列预测建模方法汇总
猜你喜欢
树莓派无需显示屏的VNC Viewer方式的远程连接
Yann Lecun's new paper: the road to building automatic agents
Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
MySQL general binary installation method
sentinel
How to use output in katalon
Mysql通用二进制安装方式
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
mysql数据库概述以及安装过程
【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)
随机推荐
Knowing the details of redis RDB, you can step on many holes less
JS基础10
爱可可AI前沿推介(6.28)
工控安全之勒索病毒篇
【功能建议】多个工作空间启动时选择某个空间
Using loops for, while, and if in katalon else、break、continue
Markdown -- basic usage syntax
什么是主链系统?
JS foundation 1-js introduction and operator
Internet of things application case of wireless module transparent transmission technology
Hystrix 部署
MySQL(三)
Move command
appliedzkp zkevm(9)中的Bytecode Proof
Information hidden in the trend chart of Hong Kong London gold market
NFT卡牌链游系统开发dapp搭建技术详情
毕业季,给初入社会的你一些建议
Guangzhou Customs supports the stable supply of food, agricultural products, traditional Chinese medicine and other civilian and biological resources to Hong Kong
MySQL general binary installation method
元宇宙系统的发展与原理介绍