简体   繁体   English

在MySQL中使用浮点数存储超过一百万的最大可能值

[英]Biggest possible value to store values over a million with floating points in MySQL

I am developing a web app in PHP and MySQL and I am trying to store a value in a row that is bigger than 1 million. 我正在用PHP和MySQL开发一个Web应用程序,并且我试图连续存储一个大于100万的值。 Whenever I do an insertion into the database, say if I try to insert 45,234,023.34 (no matter what value) it always gets truncated to 999,999.99. 每当我在数据库中进行插入时,请说如果我尝试插入45,234,023.34(无论是什么值),它总是会被截断为999,999.99。 I have chosen an unsigned long for that specific column. 我为该特定列选择了一个无符号的长整数。 I know that that is the max value of that datatype as per the following link . 我知道,这是根据以下链接该数据类型的最大值。 Also, I will never have to worry about more than 2 decimals. 另外,我永远不必担心超过2个小数。 All my operations will always be 2 decimals no matter what case it is. 无论大小写如何,我所有的运算都将始终为两位小数。

Do you guys know how to solve this? 你们知道如何解决吗? What is the correct approach in these situations? 在这些情况下正确的方法是什么?

Thank you for your time in advance. 谢谢您的宝贵时间。


Update: 更新:

Heres the SQL Code: 这是SQL代码:

create table `values` (`id` int unsigned not null auto_increment primary key, `desc` text null, `concept` text null, `value` double null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci

Please bear in mind that the the column I'm interested in is called "value". 请记住,我感兴趣的列称为“值”。 Thanks! 谢谢!

Don't use FLOAT(m,n) , only use FLOAT . 不要使用FLOAT(m,n) ,而只能使用FLOAT

FLOAT has only 7 digit of precision, so the 10-digit number "45,234,023.34" will lose the last few digits: 45234000 . FLOAT仅具有7位精度,因此10位数字“ 45,234,023.34”将丢失最后几位数字: 45234000

Perhaps you want DOUBLE . 也许您想要DOUBLE

Your query should be like this: 您的查询应如下所示:

create table `values` (`id` int unsigned not null auto_increment primary key, `desc` text null, `concept` text null, `value` DECIMAL(10,2) null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci

Ref Link: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html 参考链接: https : //dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html

Hope this will help!! 希望这会有所帮助!

At the end I used DECIMAL(12,2). 最后,我使用了DECIMAL(12,2)。 This is because as in the ref docs , this will allow me to have an integer (number to the left of the decimal point) of 10 digits (I only need 9, but I am leaving that extra space in case I decide to use a negative number) and 2 numbers to the right of the decimal point. 这是因为像在ref docs中一样 ,这将允许我有一个10位数字的整数(小数点左边的数字)(我只需要9,但是如果我决定使用负数)和小数点右边的2个数字。 Tested it throughly in PHP and all operations (ONLY with 2 decimals, anything bigger will be TRUNCATED!) come up right. 在PHP中进行了全面测试,所有操作(仅使用2个小数,将截断更大的字符!)就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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