如果在浮动中处理钱是不好的,为什么money_format()会这样做呢? [英] If dealing with money in a float is bad, then why does money_format() do it?

查看:181
本文介绍了如果在浮动中处理钱是不好的,为什么money_format()会这样做呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在讨论如何处理PHP中的货币显示和数学问题,并且很久以来一直使用 DECIMAL 类型将其存储在MySQL中,使用 money_format()将其格式化以便在网页上显示。但是,今天我看了一下实际的原型:

$ p $ string $ money_format $ string $

现在我有点困惑了。我被告知的是,避免花钱!但在这里,它是基本的格式化功能(比如快五倍),将输入转换为浮点数。 number_format()也是这样。

所以我的问题是:


$ $ b


  1. 除非我正在处理分数美分或数万亿美元(而且我都没有处理),我应该关心的是显示 存储(但从来没有做数学)已经被投到浮动的货币?我会接近浮点不准确的地方吗?

  2. 为什么 money_format()以这种方式构建?


除非我正在处理小数
美分或者数万亿美元(而我是
都不处理),否则我应该是
关心所有关于显示和
存储(但从未做过数学运算)的
币种,
我将会接近
的区域吗?浮点数不准确
会改变我的数字吗?

对于纯粹的舍入/显示目的,只要绝对浮点表示错误小于0.005(所以四舍五入到最接近的分数是正确的),就是安全的。

使用IEEE 754单精度,您可以安全地获得$ 131,072.00。 ($ 131,072.01表示为131072.015625,不正确的取整)。
$ b 双精度(PHP的 float 使用)直到$ 70,368,744,177,664.01(其中还有0.015625美分)。你没有什么可担心的。


如果#1的答案是我应该
的确值得关注,那么为什么
money_format()是以这种方式构建的?


需要什么类型的 PHP没有内置的十进制类型。也没有其他许多语言。



这在为什么浮点数值如此多产?


I've been waffling on how to deal with currency display and math in PHP, and for a long time have been storing it in MySQL using the DECIMAL type, and using money_format() to format it for display on the web page. However, today I looked at the actual prototype:

string money_format ( string $format , float $number )

I'm a little confused now. All I've been told is, avoid floats for money! But here it is, the fundamental formatting function (say that five times fast), casting the input to a float. number_format() does the same.

So my questions are:

  1. Unless I'm dealing with fractional cents or trillions of dollars (and I'm dealing with neither), should I be concerned at all about displaying and storing (but never doing math on) currency that's been cast to a float? Will I ever come close to the area of having floating point inaccuracies change my figures?

  2. If the answer to #1 is that I should indeed be concerned, then why is money_format() built this way?

解决方案

Unless I'm dealing with fractional cents or trillions of dollars (and I'm dealing with neither), should I be concerned at all about displaying and storing (but never doing math on) currency that's been cast to a float? Will I ever come close to the area of having floating point inaccuracies change my figures?

For pure rounding/display purposes, you're safe as long as the absolute floating-point representation error is less than $0.005 (so that rounding to the nearest cent is correct).

With IEEE 754 single-precision, you're safe up to $131,072.00. ($131,072.01 is represented as 131072.015625, which incorrectly rounds up.)

Double precision (which PHP's float uses) doesn't fail until $70,368,744,177,664.01 (which also has .015625 for the cents). You have nothing to worry about.

If the answer to #1 is that I should indeed be concerned, then why is money_format() built this way?

What type should it take? PHP doesn't have a built-in decimal type. Nor do many other languages.

This is discussed in “Why are floating-point values so prolific?”

这篇关于如果在浮动中处理钱是不好的,为什么money_format()会这样做呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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