php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78562 money_format() returns string with incorrect number
Submitted: 2019-09-18 19:42 UTC Modified: 2019-09-20 16:54 UTC
From: candace dot camarillo at vacasa dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 7.2.22 OS: Apline Linux 3.8
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: candace dot camarillo at vacasa dot com
New email:
PHP Version: OS:

 

 [2019-09-18 19:42 UTC] candace dot camarillo at vacasa dot com
Description:
------------
---
From manual page: https://php.net/function.money-format
---
Running money_format() with the number 2040.33 returns a string with 2040.32.  Other numbers seem to work.  I have not iterated through others to determine which cause the problem.  Playing with locale and other flags makes no impact on the problem.

Test script:
---------------
echo money_format('%i', 2040.33); // returns '2040.32'

Expected result:
----------------
'2040.33'

Actual result:
--------------
'2040.32'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-18 20:56 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-09-18 20:56 UTC] requinix@php.net
Are you hardcoding 2040.33 into your script? Or did you do some math to get that number?
 [2019-09-18 21:22 UTC] candace dot camarillo at vacasa dot com
-Status: Feedback +Status: Open
 [2019-09-18 21:22 UTC] candace dot camarillo at vacasa dot com
Our production code used some math to come up with this number.  It just so happened that when we upgrading our project to PHP7, a test failed that expected the correct number.  So I only found this particular number by chance.
 [2019-09-18 21:24 UTC] candace dot camarillo at vacasa dot com
However, hard-coding this number into the money_format() method causes the error, too.
 [2019-09-18 21:28 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-09-18 21:28 UTC] requinix@php.net
Try adding round(,2) to the number before giving it to money_format.
 [2019-09-18 21:33 UTC] candace dot camarillo at vacasa dot com
-Status: Feedback +Status: Open
 [2019-09-18 21:33 UTC] candace dot camarillo at vacasa dot com
Adding round(,2) to the number gives the same problem:

echo money_format('%i', round(2040.33, 2)) 
// '2040.32'
 [2019-09-19 08:29 UTC] cmb@php.net
Works as expected on <https://3v4l.org/NiIHL>.  *Might* be a musl
issue.

Anyhow, I would not use floating point values for monetary
calculations.
 [2019-09-19 09:17 UTC] nikic@php.net
...and this is why money_format() is deprecated in PHP 7.4. You're at the mercy of your libc implementation, and most don't exactly put much effort into strfmon() functionality, that nobody outside of PHP uses anymore.
 [2019-09-19 14:29 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-09-19 14:29 UTC] requinix@php.net
So it's a libc/strfmon bug.

The intl extension can do this with NumberFormatter::formatCurrency().
https://www.php.net/manual/en/numberformatter.formatcurrency.php

Or if you don't care about currency symbols, number_format() or even sprintf().
 [2019-09-20 16:54 UTC] candace dot camarillo at vacasa dot com
Thanks so much for looking at this!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC