Fix currency format issue in PHP
P粉322319601
2023-08-08 22:10:55
<p><code>numfmt_format_currency(new NumberFormatter('en',2), 31.005, "USD")</code> This code outputs $31.00. But it should output $31.01. How to resolve this issue??? This code prints $31.00. But $31.01 should be output. how to solve this problem? ? </p><p>PHP ignores the conversion of 0.005 to 0.01 in price format. </p><p>I tried to fix Magento's currency formatting issue but found a problem with PHP</p><p><br /></p>
31.005 is actually between the two numbers, see floating point precision at https://php.net/float.
(In PHP and other languages, there is no number 31.005..)
You need to provide numbers with sufficient precision, but with as little error as possible, to match your display requirements. This can be achieved by specifying the rounding mode, for example, if you want to round 31.005 to 31.01, you can choose to round up or round down.
##
This is what droopsnoot commented