我必須把某些項目的價格放在mysql表中。創建表格時,我使用的是DECIMAL(10,2)
,因爲在逗號後我不需要超過2位數字(例如:123,45將被接受爲輸入,但123,456將用PHP四捨五入爲123,45)。在mysqli中處理十進制數
第一個問題:使用DECIMAL(10,2)
,我怎麼知道逗號前可以存儲多少個數字?我知道它不是10,因爲10是精確性,Mysql在用這些數字進行數學計算時使用的精度:所以數字的長度在哪裏指定?
第二個問題:我使用PHP對用戶輸入進行四捨五入以適合數據類型(在逗號後面用2個數字浮動)。我應該如何使用mysqli->bind_param
來插入這些數據?我應該使用哪些數據類型(從documentation)接受bind_param
(可能:爲什麼)?
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
謝謝!所以這是10 - 2,我現在明白了。 – Saturnix
如果您需要精確縮放的數字,我不建議使用'double'或'float'。見http://dev.mysql.com/doc/refman/5.6/en/problems-with-float.html –
在這個問題和答案mysql使用'decimal'。它只有被設置爲double的綁定參數類型。並且它是給定類型的唯一選項。 –