|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-08-28 13:55 UTC] matt at claritum dot com
Description: ------------ Hi, You can only format a currency to a set number of decimal places if the currency is used for the given locale - see test script and Bug #63140[1] Now I know this is probably an issue with upstream - it seems setting the currency resets the number of decimal places, and we don't have any way of resetting it afterwards. But this makes displaying unit prices a real pain. Would it be possible to expose the setCurrency() method[2]? I don't know for sure, but I have a feeling setting the currency then setting FRACTION_DIGITS would work around the problem. Thanks for your time [1] https://bugs.php.net/bug.php?id=63140&edit=2 [2] http://icu- project.org/apiref/icu4c/classicu_1_1NumberFormat.html#a9f1c46495913e2edff15e8c94e 018697 Test script: --------------- $f = new NumberFormatter('en_GB', NumberFormatter::CURRENCY); $f->setAttribute($f::FRACTION_DIGITS, 4); echo "pounds: " . $f->formatCurrency('123.45', 'GBP') echo "euros: " . $f->formatCurrency('123.45', 'EUR'); echo "digits: " . $f->getAttribute($f::FRACTION_DIGITS); Expected result: ---------------- pounds: £123.4500 euros: €123.4500 digits: 4 Actual result: -------------- pounds: £123.4500 euros: €123.45 digits: 2 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 UTC |
This is really unnatural to work with. In order to get the desired effect one has to: $f = new NumberFormatter("en-GB", NumberFormatter::CURRENCY); // Calling setTextAttribute/CURRENCY_CODE resets fraction digits $f->setTextAttribute($f::CURRENCY_CODE, "GBP"); $f->setAttribute($f::FRACTION_DIGITS, 4); echo "POUNDS: ", $f->format(123.45), PHP_EOL; $f->setTextAttribute($f::CURRENCY_CODE, "EUR"); echo "EUROS: ", $f->format(123.45), PHP_EOL; Expected Output: ---------------- POUNDS: £123.4500 EUROS: €123.4500 Actual Output: -------------- POUNDS: £123.4500 EUROS: €123.45Hi, Thanks for that. Unfortunately it still doesn't do what I want. From $ php -i: intl Internationalization support => enabled version => PECL-3.0.0 ICU version => 53.1 ICU Data version => 53.1 Directive => Local Value => Master Value intl.default_locale => no value => no value intl.error_level => 0 => 0 intl.use_exceptions => 0 => 0 php > $f = new NumberFormatter('en_GB', NumberFormatter::CURRENCY); php > $f->setAttribute($f::FRACTION_DIGITS, 4); php > echo "pounds: " . $f->formatCurrency('123.45', 'GBP'); pounds: £123.4500 php > echo "euros: " . $f->formatCurrency('123.45', 'EUR'); euros: €123.45 php > echo "digits: " . $f->getAttribute($f::FRACTION_DIGITS); digits: 4 php > echo "pounds: " . $f->formatCurrency('123.45', 'GBP'); pounds: £123.4500 So it's remembering the digits and formatting GBP correctly the second time, but still not using them for EUR! I'll try with ICU 54 which is out now and let you know if that's any better.I can confirm this is fixed when using ICU 65.1 (from remi's php73 repo): # php -i | grep ICU ICU version => 65.1 ICU Data version => 65.1 ICU TZData version => 2019c ICU Unicode version => 12.1 # php -a Interactive shell php > $f = new NumberFormatter('en_GB', NumberFormatter::CURRENCY); php > $f->setAttribute($f::FRACTION_DIGITS, 4); php > echo "pounds: " . $f->formatCurrency('123.45', 'GBP') . "\n"; pounds: £123.4500 php > echo "euros: " . $f->formatCurrency('123.45', 'EUR') . "\n"; euros: €123.4500 php > echo "digits: " . $f->getAttribute($f::FRACTION_DIGITS) . "\n"; digits: 4 It took a while, but good to see at last :)