简体   繁体   中英

MySQL convert data in float field to decimal field

I have some data in a MySQL table that was mistakenly stored as a float data type. For example:

7.45

I need to convert that data to a decimal type so that I don't run into rounding errors going forward.

If I do this:

ALTER TABLE `invoice_line`
CHANGE `line_quantity` `line_quantity` decimal(18,6) unsigned NOT NULL AFTER `line_rate`;

I end up with values like this in my table:

70.449997

How can I correctly convert the data over to decimal without mangling the data?

I would use something like mysqldump (or MySQL Workbench has a utility as well) to dump the table. Then do your ALTER and recreate it. Just adding precision is going to introduce errors like this because floating point numbers are generally imprecise anyways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM