php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67718 Currency type
Submitted: 2014-07-30 13:02 UTC Modified: 2021-01-07 13:38 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: dorin dot marcoci at gmail dot com Assigned:
Status: Suspended Package: *Programming Data Structures
PHP Version: master-Git-2014-07-30 (Git) OS:
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: dorin dot marcoci at gmail dot com
New email:
PHP Version: OS:

 

 [2014-07-30 13:02 UTC] dorin dot marcoci at gmail dot com
Description:
------------
Implement new type "Currency" as many other languages have it.
It means fixed decimals (in most cases 4) and exact precision.
Or to be more flexible Decimal datatype with default/optional precision.
Internally to store these values as bigints.

When operating with money values, floats suffers by definition and bcmath is little annoying. Want to use calculations/operators in a classical way.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-30 18:26 UTC] fa@php.net
Just out of curiosity, where did you get the 4 decimals from?

To be fair, the only time I had to do with currencies a lot was when we got EUR, and the official conversion rate from DM was to 5 digits.

Then again, where would such a type live? As PHP is not strongly typed, it probably does not make sense to add some scalar like int/string.

I don't think this makes a lot of sense, but if someone happens to be in the mood to implement something.. we'll see.
 [2014-07-30 18:51 UTC] aharvey@php.net
Realistically, this would have to come in via an RFC with a patch/PR (and it would probably make more sense to implement it as a better bcmath API, rather than a currency-specific thing).

I don't mind leaving this open, but an open ticket here isn't going to get it done.
 [2014-07-30 19:30 UTC] dorin dot marcoci at gmail dot com
4 decimals probably from Object/Free Pascal ;)
http://www.freepascal.org/docs-html/ref/refsu6.html

Some time ago I translated into PHP one "bank" algorithm, related to interest calculation, loan repayment graphics etc... So, it was really difficult, notwithstanding complicated algorithm itself (PMT, VF ...), another problem was precision. I meet a lot of questions/confusions on support forums from php coders, like 0.1 + 0.2 != 0.3 and so on.

So implementing Currency type will be a real useful thing not just in simple conversion rates. In most cases business logic/calculations are done at DB level (SQL have "decimal" beside "float") from POST, GET params, which are in fact strings. But when you need do some currency calculations, statistical analysis on php layer, this will be a real improvement.

Yes, PHP is not strongly typed. Many advantages and some disadvantages.
Anyway, we have settype function, or as an idea, to declare explicitly precision like $a = 0.2000; or something like this: $a = (decimal(5)) 1.2; where 5 is precision;
 [2021-01-07 13:38 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2021-01-07 13:38 UTC] cmb@php.net
This feature clearly requires discussion, for which this bug
tracker is not suitable, and finally the RFC process[1].  Anybody
who is interested in this feature is welcome to start that
process.  For the time being, I'm suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC