#Int vs Decimal

28 messages · Page 1 of 1 (latest)

lavish crater
#

Guys, when it comes to monetary transactions. Do you advise using integers or decimal? I know the difference between one and the other, but I don't know which would be the best and why it would be the best. Remembering that I need to deal with decimal places.

random slate
#

Decimal has an advantage in form that it do not limit you to single monetary unit (not all currencies divide into 100s)

mental spruce
random slate
#

Also, in some monetary computations you need fractions of minimal monetary unit

lavish crater
strange dragon
#

thing is, Decimal requires a certain standardization in the projects. These days I did the following: #Decimal<0.5> == #Decimal<0.5000> and got a false as a result

#

with int numbers we don't have this problem since all right 0s are ignored after the comma

#

but I found out Decimal.equal?

#

rs

mental spruce
strange dragon
#

yep

#

that's true about float, but not for int

mental spruce
#

Yeah, that’s why in my initial comment I said to avoid using floats (decimals are a float) for money

strange dragon
#

btw, a very famous digital bank here in Brazil had problems with this recently

#

it was impossible to transfer 17.99R$

#

the system rounded the value to 17.98 every time

#

😂

random slate
mental spruce
random slate
livid lance
random slate
#

If anything, decimals can mean IEEE 754 decimal format, which is similar to what Decimal uses internally, but decimals are perfectly fine for money storage, as it avoid most of the issues related to the rounding coming from the binary encoding

#

The problem with IEEE 754 binary for money comes not from the fact that it is floating point, but from the fact that it has limited precision and that it uses base 2 for computations. If you have unlimited precision floating points in base 10, then there is no issue

mental spruce
#

TIL. looked into the Decimal package itself, it uses some pretty clever tricks to avoid ever dividing numbers and multiplies with multiplicands and their base10 exponents to compute new significant figures and exponents

random slate
#

Yes, because division is super costly in general sense. In x86-64 assembly it is faster to add hundreds of integers than to divide just two.

#

And that is even without SIMD magic

ruby depot