Results 1 to 4 of 4
  1. #1
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default Using double for money is a sin

    Never use float or double for money or currencies. Use fixed point instead.
    Here is a trivial program that shows you how you can not get floating point numbers to add properly.

    Changing it from float to double changes where the error occurs, but its still wrong. Testing this is an exercise left to the student.

    Java Code:
    /*
     * Copyright (c) 2008, Pat Farrell All rights reserved.
     */
    
    package com.pfarrell.utils.math;
    
    /**
     * The <code>FloatGrok</code> class implements sample code to show how floating point
     * numbers don't work the way you expect
     * 
     * @author pfarrell
     */
    public class FloatGrok {
    public FloatGrok() {
    }
    /** 
     * do the test
     */
    public void process() {
        float delta = 0.01F;
        float sum = 0;
        for (int i = 0; i < 100; i++) {
            float calc = ((float) i) * delta;
            if (sum != calc) {
                System.out.printf("i: %d sum: %f != calc: %f\n", i, sum, calc);
                break;
            }
            sum += delta;
        }
    }
        /**
         * usual shell program entry point
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            FloatGrok fg = new FloatGrok();
            fg.process();
        }
    
    }

  2. #2
    JDCAce's Avatar
    JDCAce is offline Member
    Join Date
    Oct 2008
    Location
    Columbus, GA, US
    Posts
    29
    Rep Power
    0

    Default

    I have seen some odd stuff with float and double, but I've never heard of fixed point.
    "Things are made of littler things that jiggle."

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    It is well supported in Cobol, but no language that is cool has bothered with it.

    Most Java or other languages that deal with this use an implicit fixed point. That is what you get when you use pennies for US currency.

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Here is a nice description of some of the issues. Its from a Python book, but that's fine, the issue is not Java or Python specific, its really a limitation of how float and double are implemented on modern computers.

    An Introduction to Python - Floating Point Arithmetic Issues and Limitations

Similar Threads

  1. Money Conversion - Proofreading pls?
    By javanewbie in forum New To Java
    Replies: 5
    Last Post: 08-17-2008, 01:40 PM
  2. Replies: 1
    Last Post: 08-09-2008, 12:10 PM
  3. Money to Words
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-30-2008, 04:27 AM
  4. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM
  5. transforming double to int
    By AlejandroPenton in forum New To Java
    Replies: 2
    Last Post: 12-11-2007, 01:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •