Floating point is bad for storing currency values such as 3.33 or 3.10. Because performing math on floating point loses precision due, for example: 74.20+153.20==227.40
is TRUE
in real life, but FALSE
in R
.
This Q&A thread talks about making a 'cents' field. Such that dollars_float 123.45 becomes cents_int 12345.
Why not use Double or Float to represent currency?
A solution that works in just about any language is to use integers instead, and count cents. For instance, 1025 would be $10.25. Several languages also have built-in types to deal with money. Among others,
Java
has theBigDecimal
class, andC#
has thedecimal
type.
How can we make an R
class to store currency as an integer?
Would be a nice bonus if the class had a print method to automatically printed in a nice format like 2,222.22.