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

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

        <bdo id='DFbuU'></bdo><ul id='DFbuU'></ul>
    2. 防止 Java 中 String.format("%.2f", doubleValue) 的

      时间:2023-09-29
        <legend id='WSFl3'><style id='WSFl3'><dir id='WSFl3'><q id='WSFl3'></q></dir></style></legend>

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

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

              • 本文介绍了防止 Java 中 String.format("%.2f", doubleValue) 的舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何防止 String.format("%.2f", doubleValue); 舍入(四舍五入算法)而不是截断它?

                How do I prevent String.format("%.2f", doubleValue); from rounding off (round half up algorithm) instead of just truncating it?

                例如

                doubleValue = 123.459
                

                格式化后,

                doubleValue = 123.46
                

                我只想丢弃最后一位,

                123.45
                

                我知道还有其他方法可以做到这一点,我只想知道这是否可以使用 String.format.

                I know there are other ways to do this, I just want to know if this is possible using the String.format.

                推荐答案

                你可以随时设置舍入模式:

                You can always set the rounding mode:

                http://java.sun.com/javase/6/docs/api/java/math/RoundingMode.html

                然后使用 String.Format()默认使用 HALF_EVEN,但您可以将其更改为 CEILING

                and then use String.Format() HALF_EVEN is used by default, but you can change it to CEILING

                另一种不太灵活的方法是(但这不是你问的):

                another no so flexible approach will be (but this is not what you asked about):

                DecimalFormat df = new DecimalFormat("###.##");
                df.format(123.459);
                

                这篇关于防止 Java 中 String.format("%.2f", doubleValue) 的舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在Java中打印带有2位小数的整数 下一篇:用于检查字符串是否包含*无*异常的数字的 Java

                相关文章

                最新文章

              • <small id='pSfBO'></small><noframes id='pSfBO'>

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