A decimal type suddenly becomes float

Rails 3.1.3

I have a field of type,

t.decimal "startp", :precision => 4, :scale => 1

which is of course, 13.2, 44.5, 123.9, and so on.

Once in a while, some of the values, which are either already stored in
DB or newly created, become something like,

9.80000000000000001 or 65.40000000000000000001

which was supposed to be 9.8 and 65.4 respectively (I did not count the
number of zeros precisely).

It does not happen all the time, it just does once in a while, and I am
not sure what’s causing it.

Could anyone guess the cause of this issue?

soichi

On 19 March 2012 00:41, Soichi I. [email protected] wrote:

DB or newly created, become something like,
Could anyone guess the cause of this issue?
It may be to do with how you are displaying the value that is causing
it to be converted to a float before being displayed. How are you
displaying the value in order to see it like that?

Colin

On Mon, Mar 19, 2012 at 9:05 AM, Colin L. [email protected]
wrote:

Once in a while, some of the values, which are either already stored in

Could anyone guess the cause of this issue?

It may be to do with how you are displaying the value that is causing
it to be converted to a float before being displayed. How are you
displaying the value in order to see it like that?

It may also have to do with the way the value is assigned to the
ActiveRecord setter method.

suppose you had a class “Race” with startp as one of the
decimal column, then this

race.startp = 9.8

will first make a Float 9.8 and then assign that Float to the
BigDecimal value.

You may try:

race.startp = “9.8” # you could use the user supplied string

race.startp = BigDecimal.new(“9.8”)

HTH,

peter