比较 int 和 Number 的最佳方法
我有一个带有 Number 参数的方法,并且必须确定它是否小于 int
。我想出的是这样的:Integer.valueOf(myInt) > (Integer) myNumber
对于这样一个简单的任务来说,这看起来相当笨拙。此外,我不确定它是否能很好地与 BigDecimal 等一起使用,以及我必须测试哪些情况?
如何改进?
谢谢
I have a method with a Number parameter and have to determine if it is smaller than an int
. What I came up with is this :
Integer.valueOf(myInt) > (Integer) myNumber
Which looks rather clumsy for such a simple task. Moreover, I am not sure if it would play nicely with BigDecimal
et.al., and what cases would I have to test?
How could it be improved?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果
myNumber
不是Integer
,您的代码将导致ClassCastException
。我想说,这最有可能正确处理所有
Number
类型:因为
double
具有您可以转换Number< 的所有类型中最广泛的范围。 /code> 到,并且它不会截断分数。
Your code will result in a
ClassCastException
if themyNumber
is anything but anInteger
.I'd say this has the best chance of dealing correctly with all
Number
types:because
double
has the widest range of all the types you can convert aNumber
to, and it will not truncate fractions.如果
myNumber
是Long
的实例,您可能会遇到问题。您最终可能会遇到溢出问题(如果您的Number
实际上大于Integer.MAX_VALUE
怎么办?)。此外,您的
Number
可能是double
,并且转换为int
会导致精度损失,因为您的数字将被截断。将您的数字转换为
double
可能是一个合理的解决方案:You might have a problem here if
myNumber
is an instance ofLong
. You might end up having overflow issues (what if yourNumber
is actually greater thanInteger.MAX_VALUE
?).Also, your
Number
could be adouble
, and a conversion toint
would cause a loss of precision, as your number would be truncated.Converting your number to
double
could be a reasonable solution:但这会丢失信息。
But that would drop information.
比较有点棘手,因为 BigDecimal 和 BigInteger 也扩展了 Number。这些类可以保存无限大小的整数值(当然,受计算机内存的限制)。
因此,如果您要求这些值的 double 值或 long 值,您可能会面临错误比较的风险,因为 BigDecimal 或 BigInteger 将被迫截断它们的值。
最安全的做法是将 Number 转换为 String,然后将此 String 交给 BigDecimal 类进行解析。
例如。
如果您确定永远不会获得其值超过双精度值的最大正值或最大负值的 BigInteger 或 BigDecimal 实例,那么使用 Number.doubleValue 获取要比较的数字应该是安全的。
由于必须比较 BigDecimals,您可能会面临其他问题。这是因为 BigDecimals 以 10 为基数表示其值,而其他 Number 子类则使用 2 为基数。
因此
new BigDecimal("0.1")
不等于0.1d
或 <代码>0.1f。这是因为浮点数和双精度数无法准确表示许多以 10 为基数的小数(而 BigDecimal 可以)。因此,从 BigDecimal 获取双精度值可能最终会给您带来错误的比较。但由于您在一个不需要面对的问题中与整数进行比较。The comparison gets a little tricky because BigDecimal and BigInteger also extend Number. These classes can hold integer values of unlimited size (well, limited by the memory on your computer).
Thus, if you ask for for the double value or long value of these you may risk erroneous comparisons since BigDecimal or BigInteger will be forced to truncate their value.
The safest thing to do would be to convert the Number to a String and then give this String to the BigDecimal class to parse.
eg.
If you're sure you will never get an instance of BigInteger or BigDecimal whose value exceeds the maximum positive or maximum negative value of a double then it should be safe to use Number.doubleValue to get a number to compare to.
There are additional problems that you may face with the possibility of having to compare BigDecimals. This is because BigDecimals represent their values in base 10, whereas other Number subclasses use base 2.
As such
new BigDecimal("0.1")
is not equal to0.1d
or0.1f
. This is because floats and doubles cannot represent many base 10 fractions accurately (whereas BigDecimal can). So getting a double value from a BigDecimal may end up giving you erroneous comparisons. But since you are comparing to ints this in a problem that you do not need to face.