Java 如何在 BigDecimal 上使用 >、=、< 等比较运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34677644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use comparison operators like >, =, < on BigDecimal
提问by user3127109
I have a domain class with unitPrice set as BigDecimal data type. Now I am trying to create a method to compare price but it seems like I can't have comparison operators in BigDecimal data type. Do I have to change data type or is there other way around?
我有一个域类,其 unitPrice 设置为 BigDecimal 数据类型。现在我正在尝试创建一种比较价格的方法,但似乎我无法在 BigDecimal 数据类型中使用比较运算符。我必须更改数据类型还是有其他方法?
采纳答案by Simulant
Every object of the Class BigDecimal
has a method compareTo
you can use to compare it to another BigDecimal. The result of compareTo
is then compared > 0
, == 0
or < 0
depending on what you need. Read the documentation and you will find out.
Class 的每个对象BigDecimal
都有一个方法compareTo
可以用来将它与另一个 BigDecimal 进行比较。compareTo
然后比较的结果> 0
,== 0
或者< 0
根据您的需要进行比较。阅读文档,你会发现。
The operators ==
, <
, >
and so on can only be used on primitive data types like int
, long
, double
or their wrapper classes like Integer
and Double
.
运营商==
,<
,>
等只能在基本数据类型等中使用int
,long
,double
或它们的包装类等Integer
和Double
。
From the documentation of compareTo
:
从文档compareTo
:
Compares this
BigDecimal
with the specifiedBigDecimal
.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.Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
将此
BigDecimal
与指定的BigDecimal
.
BigDecimal
值相等但具有不同比例(如 2.0 和 2.00)的两个对象被此方法视为相等。对于六个布尔比较运算符(<、==、>、>=、!=、<=)中的每一个,此方法优先于单独的方法提供。执行这些比较的建议惯用语是:(x.compareTo(y) <op> 0)
, where<op>
是六个比较运算符之一。返回: -1、0 或 1,因为此 BigDecimal 在数字上小于、等于或大于 val。
回答by Arnaud
Use the compareTo
method of BigDecimal :
使用compareTo
BigDecimal的方法:
public int compareTo(BigDecimal val) Compares this BigDecimal with the specified BigDecimal.
Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
public int compareTo(BigDecimal val) 将此 BigDecimal 与指定的 BigDecimal 进行比较。
Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
回答by Mureinik
BigDecimal
isn't a primitive, so you cannot use the <
, >
operators. However, since it's a Comparable
, you can use the compareTo(BigDecimal)
to the same effect. E.g.:
BigDecimal
不是原语,因此您不能使用<
,>
运算符。但是,由于它是Comparable
,您可以使用compareTo(BigDecimal)
达到相同的效果。例如:
public class Domain {
private BigDecimal unitPrice;
public boolean isCheaperThan(BigDecimal other) {
return unitPirce.compareTo(other.unitPrice) < 0;
}
// etc...
}
回答by Agung Setiawan
You can use method named compareTo
, x.compareTo(y)
. It will return 0 if x and y are equal, 1 if x is greater than y and -1 if x is smaller than y
您可以使用名为compareTo
, 的方法x.compareTo(y)
。如果 x 和 y 相等,则返回 0,如果 x 大于 y,则返回 1,如果 x 小于 y,则返回 -1
回答by torina
To be short:
简而言之:
firstBigDecimal.compareTo(secondBigDecimal) < 0 // "<"
firstBigDecimal.compareTo(secondBigDecimal) > 0 // ">"
firstBigDecimal.compareTo(secondBigDecimal) == 0 // "=="
firstBigDecimal.compareTo(secondBigDecimal) >= 0 // ">="
回答by Alexey
Using com.ibm.etools.marshall.util.BigDecimalRangeutil class of IBM one can compare if BigDecimalin range.
使用IBM 的com.ibm.etools.marshall.util.BigDecimalRangeutil 类可以比较BigDecimal 是否在范围内。
boolean isCalculatedSumInRange = BigDecimalRange.isInRange(low, high, calculatedSum);
回答by ognjenkl
Here is an example for all six boolean comparison operators (<, ==, >, >=, !=, <=):
以下是所有六个布尔比较运算符(<、==、>、>=、!=、<=)的示例:
BigDecimal big10 = new BigDecimal(10);
BigDecimal big20 = new BigDecimal(20);
System.out.println(big10.compareTo(big20) < -1); // false
System.out.println(big10.compareTo(big20) <= -1); // true
System.out.println(big10.compareTo(big20) == -1); // true
System.out.println(big10.compareTo(big20) >= -1); // true
System.out.println(big10.compareTo(big20) > -1); // false
System.out.println(big10.compareTo(big20) != -1); // false
System.out.println(big10.compareTo(big20) < 0); // true
System.out.println(big10.compareTo(big20) <= 0); // true
System.out.println(big10.compareTo(big20) == 0); // false
System.out.println(big10.compareTo(big20) >= 0); // false
System.out.println(big10.compareTo(big20) > 0); // false
System.out.println(big10.compareTo(big20) != 0); // true
System.out.println(big10.compareTo(big20) < 1); // true
System.out.println(big10.compareTo(big20) <= 1); // true
System.out.println(big10.compareTo(big20) == 1); // false
System.out.println(big10.compareTo(big20) >= 1); // false
System.out.println(big10.compareTo(big20) > 1); // false
System.out.println(big10.compareTo(big20) != 1); // true
回答by Adil Hussain
This thread has plenty of answers stating that the BigDecimal.compareTo(BigDecimal)method is the one to use to compare BigDecimalinstances. I just wanted to add for anymore not experienced with using the BigDecimal.compareTo(BigDecimal)method to be careful with how you are creating your BigDecimalinstances. So, for example...
该线程有很多答案表明BigDecimal.compareTo(BigDecimal)方法是用于比较BigDecimal实例的方法。我只是想补充一下没有使用BigDecimal.compareTo(BigDecimal)方法的经验,以注意如何创建BigDecimal实例。所以,例如...
new BigDecimal(0.8)
will create aBigDecimal
instance with a value which is notexactly0.8
and which has a scale of 50+,new BigDecimal("0.8")
will create aBigDecimal
instance with a value which isexactly0.8
and which has a scale of 1
new BigDecimal(0.8)
将创建一个BigDecimal
值不完全正确0.8
且比例为 50+的实例,new BigDecimal("0.8")
将创建一个BigDecimal
实例与值是准确0.8
和其中具有1的规模
... and the two will be deemed to be unequal according to the BigDecimal.compareTo(BigDecimal)method because their values are unequal when the scale is not limited to a few decimal places.
...并且根据BigDecimal.compareTo(BigDecimal)方法,两者将被视为不相等,因为当比例不限于几个小数位时,它们的值是不相等的。
First of all, be careful to create your BigDecimal
instances with the BigDecimal(String val)
constructor or the BigDecimal.valueOf(double val)
method rather than the BigDecimal(double val)
constructor. Secondly, note that you can limit the scale of BigDecimalinstances prior to comparing them by means of the BigDecimal.setScale(int newScale, RoundingMode roundingMode)method.
首先,BigDecimal
使用BigDecimal(String val)
构造函数或BigDecimal.valueOf(double val)
方法而不是BigDecimal(double val)
构造函数创建实例时要小心。其次,请注意,您可以在比较BigDecimal实例之前通过BigDecimal.setScale(int newScale, RoundingMode roundingMode)方法限制它们的比例。