zoukankan      html  css  js  c++  java
  • Effective Java 48 Avoid float and double if exact answers are required

    Reason

    The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.

       

    // This will print 0.6100000000000001

    System.out.println(1.03 - .42);

       

    // This will print 0.09999999999999998

    System.out.println(1.00 - 9 * .10);

       

    Right way

    Use BigDecimal , int , or long for monetary calculations

       

    public static void main(String[] args) {

    final BigDecimal TEN_CENTS = new BigDecimal( ".10");

    int itemsBought = 0;

    BigDecimal funds = new BigDecimal("1.00");

    for (BigDecimal price = TEN_CENTS; funds.compareTo(price) >= 0; price = price.add(TEN_CENTS)) {

    itemsBought++;

    funds = funds.subtract(price);

    }

    System.out.println(itemsBought + " items bought.");

    System.out.println("Money left over: $" + funds);

    }

       

    Advantage

    1. Precise
    2. Full control over Rounding(Able to select from eight rounding modes whenever an operation that entails rounding is performed)

    Disadvantage

    1. less convenient than using a primitive arithmetic type
    2. It's slower

       

    An alternative to using BigDecimal

    Using int or long, depending on the amounts involved, and to keep track of the decimal point yourself.

       

    public static void main(String[] args) {

    int itemsBought = 0;

    int funds = 100;

    for (int price = 10; funds >= price; price += 10) {

    itemsBought++;

    funds -= price;

    }

    System.out.println(itemsBought + " items bought.");

    System.out.println("Money left over: "+ funds + " cents");

    }

       

    Decision on choosing implementation

    Quantities

    9

    18

    >18

    Use

    int

    long

    BigDecimal

       

    Summary

    Don't use float or double for any calculations that require an exact answer. Use BigDecimal if you want the system to keep track of the decimal point and you don't mind the inconvenience and cost of not using a primitive type.

  • 相关阅读:
    Python安装(小白教程)中文版Pycharm
    二叉树遍历1
    node* p 和 node *p 和 node * p 的区别
    WinForm中的ListBox组件编程
    C# winform listBox中的项上下移动(转)
    C# ListBox 左移、右移、上移、下移
    C#上移,下移TreeView中的树节点顺序
    C#遍历DataSet与DataSet元素实现代码
    C# 手动编写 DataSet,DataTable 及遍历DataSet中的数据
    【.NET】C#中遍历各类数据集合的方法
  • 原文地址:https://www.cnblogs.com/haokaibo/p/avoid-float-and-double-if-exact-answers-are-required.html
Copyright © 2011-2022 走看看