Java 中 Number 对象的模
有没有安全的方法可以在 Java 中对 Number 类型的对象应用模函数?
尝试采用“数字通用”方式来对集合应用过滤器。
Are there any safe ways to apply the modulus-function on objects of type Number in Java?
Trying to make a "Number-generic" way to apply a filter on a collection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一的通用方法将涉及仅使用抽象
Number
类中定义的方法,因此,如果您所说的“安全”是指不丢失精度,那么就不会,因为Number 中定义的所有方法
警告它们可能会舍入或截断结果(floatValue()
和doubleValue()
除外,它们仅舍入)。The only generic approach would involve working only with methods defined in the abstract
Number
class, so if by "safe" you mean without losing precision, then no, because all of the methods defined inNumber
warn that they may round or truncate the result (except forfloatValue()
anddoubleValue()
, which only round).