sscirrus
sscirrus

Reputation: 56719

Does changing data type decimal to float cause data loss?

I need to change a couple of fields in my database from:

:decimal, :precision => 8, :scale => 5

to:

:float

Does this migation result in data loss? The current data consists of integers between 0 and 999.

If this migration will impact these numbers already stored, how can I keep this data safe?

Setup: Ruby on Rails 3 running on Heroku (solution would need to work for both PostgreSQL and MySQL).

Upvotes: 0

Views: 1230

Answers (2)

Denis de Bernardy
Denis de Bernardy

Reputation: 78463

It will, if you ever need to do exact comparisons or floating point arithmetics on your numbers. Open up PostgreSQL and try this:

select floor(.8::float * 10.0::float); -- 8
select floor((.1::float + .7::float) * 10.0::float); -- 7

See this related question for the reason:

Why do simple doubles like 1.82 end up being 1.819999999645634565360?

Upvotes: 1

zsalzbank
zsalzbank

Reputation: 9857

Integers between 0 and 999 will fit in either and the data won't be impacted. If it is just integers - why not use ints?

Upvotes: 0

Related Questions