在 MySQL 中存储货币值的最佳数据类型 [英] Best data type to store money values in MySQL

查看:35
本文介绍了在 MySQL 中存储货币值的最佳数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 MySQL 数据库中存储许多记录.它们都包含货币价值.但是我不知道每个数字会插入多少个数字.
为此,我必须使用哪种数据类型?
VARCHAR 还是 INT(或其他数字数据类型)?

I want to store many records in a MySQL database. All of them contains money values. But I don't know how many digits will be inserted for each one.
Which data type do I have to use for this purpose?
VARCHAR or INT (or other numeric data types)?

推荐答案

由于货币需要精确表示,因此不要使用像 float 这样仅近似的数据类型.您可以为此使用定点数字数据类型

Since money needs an exact representation don't use data types that are only approximate like float. You can use a fixed-point numeric data type for that like

decimal(15,2)

  • 15 是精度(值的总长度包括小数位)
  • 2 是小数点后的位数
    • 15 is the precision (total length of value including decimal places)
    • 2 is the number of digits after decimal point
    • 参见 MySQL 数字类型:

      这些类型用于保持精确精度很重要的情况,例如货币数据.

      These types are used when it is important to preserve exact precision, for example with monetary data.

      这篇关于在 MySQL 中存储货币值的最佳数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆