float or double?
This is a discussion on best variable to use for money within the C++ Programming forums, part of the General Programming Boards category; float or double?...
float or double?
Neither. Fractional powers of 10 (eg 0.1, 0.01) cannot be represented in any floating point type. So, if you use a floating point type to represent a decimal monetary type (eg dollars made up of 100 cents, pound made up of 100 pence) there will be an error of representation. That error tends to propagate and grow with various operations, such as addition or multiplication, when repeated a significant number of times.
Better to use some representation that allows precise representation of the various components. I'll leave working out options as an exercise.
Right 98% of the time, and don't care about the other 3%.