当你将$valueAsCents = 54780/100分开时,它就变成了一个浮点数,由于它们的存储方式,它在数字形式上并不总是准确的.在我的测试中,我得到了
547.7999999999999545252649113535881042480468750000
当乘以100时,这将是
54779.9999999999927240423858165740966796870000
这就是int值为54779的原因
此外,PHP manual for float type还包含一个提示,浮点数可能无法达到您的预期.
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118….