PHP 和 Money,将钱兑换成美分
因此,我对如何在数据库中存储“钱”进行了相当多的研究,我认为我想要使用的系统是
将钱转换为美分,然后将美分存储在字段类型为 DECIMAL (19 ,4)。
我的问题是,如果我有用户的输入字段......我如何处理多种输入类型。 IE:
$input = "1,346.54"
$input = "10,985.23"
$input = "110,400.50"
$input = "1033.44"
等等等等...
将其转换为 CENTS 的最佳方法是什么?因为我们必须处理“字符串”并将它们转换为 INT,然后除以 100...我尝试的所有操作都会由于与数字的“逗号”分隔而引发问题。
任何想法或方向将不胜感激。
So I have done a fair bit of research on how to store "money" in a database and I think the system I want to use is
Converting Money into CENTS and then storing the CENTS in a MySQL DB with a field type of DECIMAL (19,4).
My question is, IF I have an input field from the user... how do I deal with multiple input types.
IE:
$input = "1,346.54"
$input = "10,985.23"
$input = "110,400.50"
$input = "1033.44"
etc etc...
What would be the best method for converting this to CENTS? As we have to deal with 'strings' and convert them to INT, and divide by 100... Everything that I try throws issues because of the "comma" separation with the numbers.
Any thoughts or direction would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看起来有一个 NumberFormatter 类提供了一个 parseCurrency 方法。看看 http://www.php.net/manual/en/numberformatter .parsecurrency.php
提供的示例是
Looks like there is a NumberFormatter class which provides a
parseCurrency
method. Have a look at http://www.php.net/manual/en/numberformatter.parsecurrency.phpThe example provided is
您可以像这样删除逗号:
此时,您可以通过转换为浮点数并乘以 100 来转换为分。但是,这可能是不必要的。在执行数学运算时,您可能会遇到精度问题,但只需将值存储在数据库中就可以以原始形式完成,而无需更改值(假设您的数据库表结构正确):
You can remove the commas like this:
At this point, you can convert to cents by converting to a float and multiplying by 100. However, this is probably unnecessary. You would potential encounter precision issues when performing math operations, but simply storing the values in the database can be done in the original form without alteration of the value (assuming your DB tables are properly structured):
这会将大多数十进制货币转换为其子单位。
$1,234,567.89 = 123456789
£ 1,234,567.89 = 123456789
€1.234.567,89 = 123456789
12,34 欧元 = 1234
12,34 € = 1234
12,30 € = 1230
1,2 = 102
This converts most of the decimal currencies to their subunits.
$1,234,567.89 = 123456789
£ 1,234,567.89 = 123456789
€1.234.567,89 = 123456789
12,34 EUR = 1234
12,34 € = 1234
12,30 € = 1230
1,2 = 102
可能只是删除“,”和“。”从字符串中,结果是以美分为单位的金额。
Probably just remove the ',' and the '.' from the string, the result is the amount in cents.
您可能需要使用 strrpos 从后面解析字符串...如果您在距离末尾 2 个位置处找到逗号,那么可以安全地假设其为外币,这些是美分...一旦确定,请使用一个正则表达式来去除剩余的逗号(当然是在将“CENTS”逗号转换为小数之后)...现在您有一个正常的 DEC 数字可以使用。
使用它来查找字符串中的最后一个逗号... strrpos
使用它来替换逗号 preg_replace
这是一个有用的正则表达式网站.. regexlib
You will probably need to parse the string from the back using strrpos ... If you find a comma 2 spots from the end, then its prob safe to assume its foreign currency and those are the CENTS... Once you determine that, use a regex to strip the remaining commas (after you convert the "CENTS" comma to a decimal of course) ... Now you have a normal DEC number to play with.
Use this to find the last comma in your string ... strrpos
Use this to replace the commas preg_replace
Here is a helpful regex website .. regexlib