Tuesday, 10 September 2013

Rails: Floats, Decimals, or Integers

Rails: Floats, Decimals, or Integers

I have a rails app that needs to process some data, and some of this data
includes numbers with decimals such as 1.9943 and division between these
numbers and other integers. I just wanted to know what the best way to
store this was. Originally I thought of storing the numbers that would
stay integers as integers and numbers that could become decimals as
decimals.
This is my first time using decimals for rails, and although it was in a
weird format like
#<BigDecimal:7fda470aa9f0,'0.197757E1',18(18)>
It seems to be able to perform the correct arithmetic when I divide 2
decimal numbers or a decimal with an integer.
The thing that doesnt work correctly is when I try to divide integers with
integers. I thought rails would automatically convert the result into a
proper decimal, but it seems to keep it in integer form and strip the
remainders. Is there anything I can do about this?
And what would be the best way to store this type of information? Should I
store it all as decimals, or maybe floats

No comments:

Post a Comment