BigDecimal比较大小
2013-04-20 12:41
295 查看
BigDecimal比较大小
这个类是java里精确计算的类,下面说一下两个BigDecimal对象大小,相等的判断
1 比较对象是否相等
一般的对象用equals,但是BigDecimal比较特殊,举个例子:
BigDecimal a=BigDecimal.valueOf(1.0);
BigDecimal b=BigDecimal.valueOf(1.000);
在现实中这两个数字是相等的,但是问题来来了
a.equals(b)结果是false;怎么不相等了呢?这个问题我也不知道
解决办法:
if(a.compareTo(b)==0)结果是true
public int compareTo(BigDecimal val)
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.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
Specified by:
compareTo in interface Comparable<BigDecimal>
Parameters:
val - BigDecimal to which this BigDecimal is to be compared.
Returns:
-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
简单翻译:
结果是-1 小于
0 等于
1 大于
这个类是java里精确计算的类,下面说一下两个BigDecimal对象大小,相等的判断
1 比较对象是否相等
一般的对象用equals,但是BigDecimal比较特殊,举个例子:
BigDecimal a=BigDecimal.valueOf(1.0);
BigDecimal b=BigDecimal.valueOf(1.000);
在现实中这两个数字是相等的,但是问题来来了
a.equals(b)结果是false;怎么不相等了呢?这个问题我也不知道
解决办法:
if(a.compareTo(b)==0)结果是true
public int compareTo(BigDecimal val)
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.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
Specified by:
compareTo in interface Comparable<BigDecimal>
Parameters:
val - BigDecimal to which this BigDecimal is to be compared.
Returns:
-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
简单翻译:
结果是-1 小于
0 等于
1 大于
相关文章推荐
- BigDecimal格式比较大小
- BigDecimal 比较大小
- BigDecimal 的比较大小与加减乘除
- BigDecimal比较大小,BigDecimal保留几位小数,BigDecimal和0比较大小
- BigDecimal类型比较大小
- BigDecimal比较大小
- BigDecimal类型比较数字大小,invalue方法数值大于9位变成负数
- BigDecimal比较大小equals和compareTo方法
- java BigDecimal比较大小
- BigDecimal 加、减、乘、除、格式化、比较大小
- double由于是双精度,所以比较大小不能直接用>,<或者=,需要使用BigDecimal,具体看例子
- BigDecimal如何比较大小
- BigDecimal如何比较大小
- BigDecimal比较大小
- BigDecimal比较大小
- BigDecimal的加减乘除及比较大小
- BigDecimal类型比较大小
- BigDecimal比较大小
- BigDecimal 比较大小
- Java_BigDecimal类型比较大小