任意精度数字格式/money_format?
是否有可以使用字符串的任意精度替代 money_format而不是浮点数作为参数?
这并不是说我打算对数万亿的货币单位进行计算,而是在不滥用浮点数的情况下费尽心思正确处理货币算术之后,如果有一个函数不会在大约 15 位数字后输出随机数,那就太好了,即使用户决定提供无意义的数据。或者,嘿,也许有人想在 津巴布韦元?
我犹豫是否使用正则表达式,因为我希望利用 Money_format 的本地化。
编辑 - 找到一个可行的解决方案;见下文
Is there an arbitrary-precision alternative to money_format available that could take a string instead of a float as a parameter?
It's not that I plan on doing calculations on trillions of monetary units, but after going through the trouble to properly handle monetary arithmetic without abusing floats, it'd be nice to have a function that doesn't spew random numbers after about 15 digits, even if users decide to give it nonsense data. Or, hey, maybe someone wants to buy two sticks of gum in Zimbabwe dollars?
I hesitate to use regular expressions because I was hoping to make use of the localization of money_format.
edit - found a workable solution; see below
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 NumberFormatter 类
try the NumberFormatter class
由 PHP 网站上评论者提交的函数拼凑而成 此处 和此处。编辑以使用任意精度参数。
测试:
Cobbled together from the commenter-submitted functions on PHP's site here and here. Edited to work with arbitrary-precision parameters.
Tests: