将数据类型decimal更改为float会导致数据丢失吗?

时间:2022-10-05 16:32:18

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.

这一误导会导致数据丢失吗?当前数据由0到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).

安装:在Heroku上运行的Ruby on Rails 3(解决方案需要同时适用于PostgreSQL和MySQL)。

2 个解决方案

#1


0  

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?

0和999之间的整数都可以,数据不会受到影响。如果它只是整数-为什么不使用int ?

#2


1  

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

如果你需要对数字进行精确的比较或者浮点运算的话,它会。打开PostgreSQL并尝试以下操作:

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?

为什么像1。82这样的简单双打最终会变成1。819999999645634565360?

#1


0  

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?

0和999之间的整数都可以,数据不会受到影响。如果它只是整数-为什么不使用int ?

#2


1  

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

如果你需要对数字进行精确的比较或者浮点运算的话,它会。打开PostgreSQL并尝试以下操作:

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?

为什么像1。82这样的简单双打最终会变成1。819999999645634565360?