比较 int 和 Number 的最佳方法

发布于 2024-12-19 13:33:55 字数 240 浏览 5 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦忆晨望 2024-12-26 13:33:56

如果 myNumber 不是 Integer,您的代码将导致 ClassCastException

我想说,这最有可能正确处理所有 Number 类型:

myInt > myNumber.doubleValue()

因为 double 具有您可以转换 Number< 的所有类型中最广泛的范围。 /code> 到,并且它不会截断分数。

Your code will result in a ClassCastException if the myNumber is anything but an Integer.

I'd say this has the best chance of dealing correctly with all Number types:

myInt > myNumber.doubleValue()

because double has the widest range of all the types you can convert a Number to, and it will not truncate fractions.

失退 2024-12-26 13:33:56

如果 myNumberLong 的实例,您可能会遇到问题。您最终可能会遇到溢出问题(如果您的 Number 实际上大于 Integer.MAX_VALUE 怎么办?)。

此外,您的 Number 可能是 double,并且转换为 int 会导致精度损失,因为您的数字将被截断。

将您的数字转换为 double 可能是一个合理的解决方案:

myInt > myNumber.doubleValue();

You might have a problem here if myNumber is an instance of Long. You might end up having overflow issues (what if your Number is actually greater than Integer.MAX_VALUE?).

Also, your Number could be a double, and a conversion to int would cause a loss of precision, as your number would be truncated.

Converting your number to double could be a reasonable solution:

myInt > myNumber.doubleValue();
∞梦里开花 2024-12-26 13:33:56
myInt > myNumber.intValue()

但这会丢失信息。

(double)myInt > myNumber.doubleValue()
myInt > myNumber.intValue()

But that would drop information.

(double)myInt > myNumber.doubleValue()
枕花眠 2024-12-26 13:33:56

比较有点棘手,因为 BigDecimal 和 BigInteger 也扩展了 Number。这些类可以保存无限大小的整数值(当然,受计算机内存的限制)。

因此,如果您要求这些值的 double 值或 long 值,您可能会面临错误比较的风险,因为 BigDecimal 或 BigInteger 将被迫截断它们的值。

最安全的做法是将 Number 转换为 String,然后将此 String 交给 BigDecimal 类进行解析。

例如。

Number n = ...;
int i = ...;

BigDecimal m = new BigDecimal(n.toString());
BigDecimal j = new BigDecimal(i);

boolean result = j.compareTo(m) < 0;
// equivalent to i < n

如果您确定永远不会获得其值超过双精度值的最大正值或最大负值的 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.

Number n = ...;
int i = ...;

BigDecimal m = new BigDecimal(n.toString());
BigDecimal j = new BigDecimal(i);

boolean result = j.compareTo(m) < 0;
// equivalent to i < n

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 to 0.1d or 0.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文