<small id='z2XMD'></small><noframes id='z2XMD'>

  1. <tfoot id='z2XMD'></tfoot>
    • <bdo id='z2XMD'></bdo><ul id='z2XMD'></ul>
  2. <i id='z2XMD'><tr id='z2XMD'><dt id='z2XMD'><q id='z2XMD'><span id='z2XMD'><b id='z2XMD'><form id='z2XMD'><ins id='z2XMD'></ins><ul id='z2XMD'></ul><sub id='z2XMD'></sub></form><legend id='z2XMD'></legend><bdo id='z2XMD'><pre id='z2XMD'><center id='z2XMD'></center></pre></bdo></b><th id='z2XMD'></th></span></q></dt></tr></i><div id='z2XMD'><tfoot id='z2XMD'></tfoot><dl id='z2XMD'><fieldset id='z2XMD'></fieldset></dl></div>

      <legend id='z2XMD'><style id='z2XMD'><dir id='z2XMD'><q id='z2XMD'></q></dir></style></legend>

      如何在 Java 中比较两个数字?

      How to Compare Two Numbers in Java?(如何在 Java 中比较两个数字?)
        <tbody id='s5ww3'></tbody>

          <legend id='s5ww3'><style id='s5ww3'><dir id='s5ww3'><q id='s5ww3'></q></dir></style></legend>
              <bdo id='s5ww3'></bdo><ul id='s5ww3'></ul>

              <small id='s5ww3'></small><noframes id='s5ww3'>

              <tfoot id='s5ww3'></tfoot>
              • <i id='s5ww3'><tr id='s5ww3'><dt id='s5ww3'><q id='s5ww3'><span id='s5ww3'><b id='s5ww3'><form id='s5ww3'><ins id='s5ww3'></ins><ul id='s5ww3'></ul><sub id='s5ww3'></sub></form><legend id='s5ww3'></legend><bdo id='s5ww3'><pre id='s5ww3'><center id='s5ww3'></center></pre></bdo></b><th id='s5ww3'></th></span></q></dt></tr></i><div id='s5ww3'><tfoot id='s5ww3'></tfoot><dl id='s5ww3'><fieldset id='s5ww3'></fieldset></dl></div>
                本文介绍了如何在 Java 中比较两个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                可能重复:
                比较两个通用数字的值

                我想比较 的两个任意实例Number 找出哪个更大.如您所知,它们可能是整数、长整数或双精度数,也可能是大整数或大小数.那么该怎么办呢?

                I would like to compare two arbitrary instances of Number to find out which is greater. As you know, those may be Integers, Longs or Doubles but also BigIntegers or BigDecimals. So what to do?

                1. 编号未实现 Comparable.
                2. 不能使用 doubleValue(),因为 BigDecimals 或 BigIntegers 可能会损失精度.
                1. Number does not implement Comparable.
                2. Can't use doubleValue() as there might be a loss of precision from BigDecimals or BigIntegers.

                我真的需要进行实例测试和铸造吗?必须有一些图书馆可以做到这一点.但是我已经检查了 Apache Commons 和 Guava,但没有运气.

                Do I really have to go into instance-testing and casting? There must be some library that does this. But I've already checked Apache Commons and Guava with no luck.

                编辑 1: 评论指出了这一点:比较两个通用数字的值.它有一个解决方案,可以使用 toString() 将所有内容转换为 BigDecimal.真的没有更好的方法吗?

                Edit 1: Comments pointed this out: Comparing the values of two generic Numbers. It has a solution that converts everything to a BigDecimal using toString(). Is there really no better way to do this?

                编辑 2:我刚刚发布了我自己的版本作为另一个问题的答案.它应该比公认的答案更有效、更健壮.

                Edit 2: I just posted my own version as an answer to the other question. It should be more efficient and robust than the accepted answer.

                推荐答案

                你不能比较 Number 类型的两个任意实例,因为这可能是一个无休止的操作.想象两个无理数,唯一的区别是十亿位小数.为此,您必须比较数十亿个小数.

                You can't compare two arbitrary instances of type Number, because this is a possibly endless operation. Imagine two irrational numbers, where the only difference is in the billion-th decimal. You would have to compare some billion decimals for this.

                您需要将您的 Number 实例转换为 BigDecimal,根据需要应用舍入,然后比较这两者.

                You need to convert your Number instances to BigDecimal, applying rounding as necessary, and then compare these both.

                这篇关于如何在 Java 中比较两个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                  <tbody id='v65zb'></tbody>
                1. <small id='v65zb'></small><noframes id='v65zb'>

                  <tfoot id='v65zb'></tfoot>

                    <legend id='v65zb'><style id='v65zb'><dir id='v65zb'><q id='v65zb'></q></dir></style></legend>
                      <bdo id='v65zb'></bdo><ul id='v65zb'></ul>
                      <i id='v65zb'><tr id='v65zb'><dt id='v65zb'><q id='v65zb'><span id='v65zb'><b id='v65zb'><form id='v65zb'><ins id='v65zb'></ins><ul id='v65zb'></ul><sub id='v65zb'></sub></form><legend id='v65zb'></legend><bdo id='v65zb'><pre id='v65zb'><center id='v65zb'></center></pre></bdo></b><th id='v65zb'></th></span></q></dt></tr></i><div id='v65zb'><tfoot id='v65zb'></tfoot><dl id='v65zb'><fieldset id='v65zb'></fieldset></dl></div>