MySQL - 避免截断浮点数据类型中的尾随零
我有一列用于存储浮点数据,即
1.1
11.60
4.23
不幸的是,11.60
被存储为11.6
。我需要它有那个额外的零。我必须将数据类型更改为 varchar 吗?处理这个问题的最佳方法是什么?
I've got a column for storing float data, i.e.
1.1
11.60
4.23
Unfortunately, 11.60
gets stored as 11.6
. I need it to have that extra zero. Do I have to change my datatype to varchar? What's the best way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从评论中看来,您正在存储产品代码,因此正如您所建议的那样,浮点数对于数据类型来说不是一个好的选择。事实上,这不是渲染问题,但我们从您最初选择的浮点数中误解了它(认为您确实存储了诸如金钱或真正的小数之类的东西)。
正如您所怀疑的,使用 varchar ,因为它确实是一个字符串值。
您可以这样做:
It sounds from the comments that you're storing a product code, so float isn't a good choice for a datatype, as you suggest. Indeed it's not a rendering issue, but we'd misconstrued it from your initial choice of float (thinking you indeed were storing something like money or true decimal).
Go with varchar, as you suspected, as it really is a string value.
Here's how you can do that:
这是一个渲染问题,而不是数据问题。要“解决”它,请应用 mysql 的 FORMAT当您选择它时,函数将根据您的值进行计算:
2
是小数位数。它将处理(几乎)小数点左侧的任意数量的数字。This is a rendering issue, not a data issue. To "solve" it, apply mysql's FORMAT function to your value as you select it:
The
2
is the number of decimal places. It will handle (almost) any number of digits to the left of the decimal place.