我应该将字段PRICE存储为int还是作为float int数据库? [英] Should I store a field PRICE as an int or as a float int the database?

查看:379
本文介绍了我应该将字段PRICE存储为int还是作为float int数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个项目中,我注意到价格字段被存储为一个int,而不是一个浮点。这是通过将实际值乘以100来完成的,原因是为了避免遇到浮点问题。



这是一个很好的做法,我应该跟着还是



p>我实际上不会在mysql环境中选择float。



对我来说,选择是 int decimal(18,4)



我看过用来表示浮点值的真实世界示例整数。 JD Edwards数据表的内部都这样做。数量通常除以10000.虽然我确信它在表中更快更小,但这意味着如果我们想对它们做任何事情,我们总是需要将ints转换为十进制值,特别是除法。 / p>

从编程的角度来看,我总是喜欢用 decimal 来处理价格(或 money 在支持它的RDBMS中)。


In a previous project, I noticed that the price field was being stored as an int, rather than as a float. This is done by multiplying the actual value by 100, the reason being was to avoid running into floating point problems.

Is this a good practice that I should follow or is it unnecessary and only makes the data less transparent?

解决方案

Interesting question.

I wouldn't actually choose float in the mysql environment. Too many problems in the past with precision with that datatype.

To me, the choice would be between int and decimal(18,4).

I've seen real world examples integers used to represent floating point values. The internals of JD Edwards datatables all do this. Quantities are typically divided by 10000. While I'm sure it's faster and smaller in-table, it just means that we're always having to CAST the ints to a decimal value if we want to do anything with them, especially division.

From a programming perspective, I'd always prefer to work with decimal for price ( or money in RDBMSs that support it ).

这篇关于我应该将字段PRICE存储为int还是作为float int数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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