<bdo id='b4eOj'></bdo><ul id='b4eOj'></ul>

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

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

      2. Java Double 值 = 0.01 更改为 0.009999999999999787

        Java Double value = 0.01 changes to 0.009999999999999787(Java Double 值 = 0.01 更改为 0.009999999999999787)

      3. <i id='6Vtn5'><tr id='6Vtn5'><dt id='6Vtn5'><q id='6Vtn5'><span id='6Vtn5'><b id='6Vtn5'><form id='6Vtn5'><ins id='6Vtn5'></ins><ul id='6Vtn5'></ul><sub id='6Vtn5'></sub></form><legend id='6Vtn5'></legend><bdo id='6Vtn5'><pre id='6Vtn5'><center id='6Vtn5'></center></pre></bdo></b><th id='6Vtn5'></th></span></q></dt></tr></i><div id='6Vtn5'><tfoot id='6Vtn5'></tfoot><dl id='6Vtn5'><fieldset id='6Vtn5'></fieldset></dl></div>

              <small id='6Vtn5'></small><noframes id='6Vtn5'>

                <tbody id='6Vtn5'></tbody>
              <tfoot id='6Vtn5'></tfoot>
                <bdo id='6Vtn5'></bdo><ul id='6Vtn5'></ul>

                <legend id='6Vtn5'><style id='6Vtn5'><dir id='6Vtn5'><q id='6Vtn5'></q></dir></style></legend>
                • 本文介绍了Java Double 值 = 0.01 更改为 0.009999999999999787的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:
                  为什么不使用 Double 或 Float 来表示货币?

                  我正在为我的高中课程用 Java 编写一个基本的命令行程序.我们现在只使用变量.它用于计算购买后您找零中任何类型的纸币和硬币的数量.这是我的程序:

                  I'm writing a basic command-line program in Java for my high school course. We're only working with variables right now. It's used to calculate the amount of bills and coins of whatever type in your change after a purchase. This is my program:

                  class Assign2c {
                      public static void main(String[] args) {
                          double cost = 10.990;
                          int paid = 20;
                          double change = paid - cost;
                          int five, toonie, loonies, quarter, dime, nickel, penny;
                  
                          five = (int)(change / 5.0);
                          change -= five * 5.0;
                  
                          toonie = (int)(change / 2.0);
                          change -= toonie * 2.0;
                  
                          loonies = (int)change;
                          change -= loonies;
                  
                          quarter = (int)(change / 0.25);
                          change -= quarter * 0.25;
                  
                          dime = (int)(change / 0.1);
                          change -= dime * 0.1;
                  
                          nickel = (int)(change / 0.05);
                          change -= nickel * 0.05;
                  
                          penny = (int)(change * 100);
                          change -= penny * 0.01;
                  
                          System.out.println("$5   :" + five);
                          System.out.println("$2   :" + toonie);
                          System.out.println("$1   :" + loonies);
                          System.out.println("$0.25:" + quarter);
                          System.out.println("$0.10:" + dime);
                          System.out.println("$0.05:" + nickel);
                          System.out.println("$0.01:" + penny);
                      }
                  }
                  

                  它应该一切正常,但在最后一步,当剩下 0.01 美元时,便士的数量应该是 1,但实际上是 0.在进入代码并将更改值输出到控制台几分钟后,我已经发现在最后一步当change = 0.01时,它变为0.009999999999999787.为什么会这样?

                  It should all work but at the last step when there's $0.01 leftover, number of pennies should be 1 but instead, it's 0. After a few minutes of stepping into the code and outputting the change value to the console, I've found out that at the last step when change = 0.01, it changes to 0.009999999999999787. Why is this happening?

                  推荐答案

                  使用 double 作为货币是个坏主意,为什么不使用 Double 或 Float 来表示货币?.我建议使用 BigDecimal 或以美分计算.

                  Using double for currency is a bad idea, Why not use Double or Float to represent currency?. I recommend using BigDecimal or doing every calculation in cents.

                  这篇关于Java Double 值 = 0.01 更改为 0.009999999999999787的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 表达式)

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

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

                            <bdo id='qjRzA'></bdo><ul id='qjRzA'></ul>