storing money amounts in mysql

后端 未结 7 657
野趣味
野趣味 2020-11-29 19:36

I want to store 3.50 into a mysql table. I have a float that I store it in, but it stores as 3.5, not 3.50. How can I get it to have the trailing zero?

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 20:20

    Do not store money values as float, use the DECIMAL or NUMERIC type:

    Documentation for MySQL Numeric Types

    EDIT & clarification:

    Float values are vulnerable to rounding errors are they have limited precision so unless you do not care that you only get 9.99 instead of 10.00 you should use DECIMAL/NUMERIC as they are fixed point numbers which do not have such problems.

提交回复
热议问题