简体   繁体   English

PHP / MySQL:最佳资金运营/存储实践?

[英]PHP/MySQL: Best money operations/storing practices?

So, I am planning to make an application (PHP/MySQL) which deals a lot with money, and I am thinking about how to store and operate with the money, referring to PHP float data type and MySQL decimal. 因此,我打算制作一个应用程序(PHP / MySQL),它可以节省大量资金,我正在考虑如何存储和操作钱,参考PHP浮点数据类型和MySQL十进制。

I was thinking of two options. 我在考虑两种选择。 One of them is to operate and store money in integer cents format ($dollars * 100) in order not to deal with float inprecisions and to store it in the DB as integer too. 其中之一是以整数分钱格式($ dollar * 100)操作和存储货币,以便不处理浮动预处理并将其作为整数存储在DB中。 The other one is to store in DB as decimal and to use BC Math in PHP for calculations. 另一个是以DB为单位存储在十进制中,并在PHP中使用BC Math进行计算。

So I googled all the night to find out which is the best option to use and didn't find a clear answer. 所以我一整天都在谷歌搜索哪个是最好的选择,并没有找到明确的答案。 The only reasonable option that I've seen was the integer cents one (which I don't really like because it would imply a lot of converting from dollars to cents and viceversa before every display in the browser and before storing in the DB). 我见过的唯一合理的选择是整数美分(我不喜欢它,因为它意味着在浏览器中的每次显示之前和存储在数据库之前,很多都会从美元转换为美分和反之亦然)。

Also, people have complained about MySQL decimal (MySQL stores decimals as strings, operates them as floats etc.), but that were old posts. 此外,人们抱怨MySQL十进制(MySQL将小数存储为字符串,将它们作为浮点数等操作),但那是旧帖子。 According to MySQL documentation, the current version handles decimals correctly, the only complaint was that it truncates the fraction of the values that exceeds the declared fraction length (eg if you store a value of 12.326 in a column declared decimal(9,2)), but from my investigations it rounds it instead of just truncating (12.326 becomes 12.33), which is correct in my opinion. 根据MySQL文档,当前版本正确处理小数,唯一的抱怨是它截断超过声明的分数长度的值的分数(例如,如果在声明为十进制的列(9,2)中存储值12.326) ,但是从我的调查来看,它不仅仅是截断(12.326变为12.33),这在我看来是正确的。

And, I didn't find any recommendation on storing money as decimals and make calculations using PHP BCMath, and in my opinion this is because few people know about BC and GMP math functions. 并且,我没有找到任何关于将钱存储为小数并使用PHP BCMath进行计算的建议,并且在我看来这是因为很少有人知道BC和GMP数学函数。

So, what would be the best option to use, considering precision, speed (BCMath calculations speed, MySQL decimal speed vs integer) and programming comfort? 那么,考虑到精度,速度(BCMath计算速度,MySQL十进制速度与整数)和编程舒适度,最佳选择是什么?

I'd definitely go for using ints and routing everything through a data object (ORM) style that then handles all conversion for you. 我肯定会使用int并通过数据对象(ORM)样式路由所有内容,然后为您处理所有转换。 The client code using the data object will never need to do conversion and won't care, while you won't have problems with storage as ints are handled easily by the DB. 使用数据对象的客户端代码永远不需要进行转换,也不会关心,而您不会遇到存储问题,因为数据库可以轻松处理整数。 Furthermore, you can then add whatever other methods are needed for the money object (like conversions between money types, etc) quite easily. 此外,您可以非常轻松地添加货币对象所需的任何其他方法(如货币类型之间的转换等)。

I also had trouble finding information about BCMath, so I researched it and wrote my own article about it: http://www.exploringbinary.com/base-conversion-in-php-using-bcmath/ . 我也很难找到有关BCMath的信息,所以我研究了它并写了我自己的文章: http//www.exploringbinary.com/base-conversion-in-php-using-bcmath/

(I'm not taking the stance that you should use BCMath -- I'm just giving you information.) (我不会采取你应该使用BCMath的立场 - 我只是给你提供信息。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM