我需要将数字保存到数据库中,请参见示例。
当前我将数字保存为22.5,并在数据库中将其保存为22。
数据库中的数字设置为整数
//sometimes $number can be "33.5" or "33" or "1" or "1.003"or "03"
$sql = "INSERT INTO active (name, number) VALUES ('somename','$number')";
答案 0 :(得分:0)
If you make your number column as DECIMAL data type, it will hold the decimal point with all numbers. Integer columns cannot hold decimal points.
If it is not your requirement, better use VARCHAR as data type (But NOT RECOMENDED).
Hope this helps.
答案 1 :(得分:0)
First of all, you need to check the structure of active
table. From your question, I understand that various types of number values is going to be saved to number
column. And I guess the type of number
column in active
table is numeric for int types. For example, INT or BIGINT or any other integer types.
Here is what I'm usually doing. Just use varchar
for number
field. It might not sound like professional. But it's very easy to use. By doing so, you won't have any problem with saving numeric value to the column. I have got an similar issue before while saving latitude and longitude value to the table. And php has got many functions to get actual float or int value from string. So this is an easy and simple way to fix.
答案 2 :(得分:0)
将数字/整数/浮点数保存到数据库中最简单可靠的方法之一是将它们转换为字符串,然后将其存储到数据库中。
注意::您的数据库列类型应为字符串/文本类型。
此方法的优点之一是您可以克服int / float / double的限制。
$string_number = (string)$number;
$float_number = (float)$string_number;
答案 3 :(得分:0)