Skip to content

Commit 289ebda

Browse files
committed
ext/intl: Add NumberFormatter::CURRENCY_ISO, PLURAL, STANDARD and CASH_CURRENCY
ICU 54 and 56 adds the following formatters[^1] for currency formatting: - `CURRENCY_ISO`[^2]: ISO currency code, e.g., "USD1.00" - `CURRENCY_PLURAL`[^3]: pluralized currency name, e.g., "1.00 US dollar" and "3.00 US dollars" - `CASH_CURRENCY`[^4]: currency symbol given CASH usage, e.g., "NT$3" instead of "NT$3.23" - `CURRENCY_STANDARD`[^5]: currency symbol, e.g., "$1.00", using non-accounting style for negative values (e.g. minus sign) Ref: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html This adds support for all four of them to ext/intl, along with tests. [^1]: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html [^2]: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html#a4eb4d3ff13bd506e7078b2be4052266daae232c48e579c727525855cd21571033 [^3]: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html#a4eb4d3ff13bd506e7078b2be4052266da3916bb92d0784396ea2331d4f04c03f5 [^4]: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html#a4eb4d3ff13bd506e7078b2be4052266da8da9eba1a27d5734599709c137c3b82f [^5]: https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/unum_8h.html#a4eb4d3ff13bd506e7078b2be4052266dac57cfff1b245d11774e8b109b98eedc2
1 parent 33c4ca3 commit 289ebda

File tree

4 files changed

+211
-1
lines changed

4 files changed

+211
-1
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ PHP 8.5 UPGRADE NOTES
119119
- DOM:
120120
. Added Dom\Element::$outerHTML.
121121

122+
- Intl:
123+
. Added NumberFormatter::CURRENCY_ISO, NumberFormatter::CURRENCY_PLURAL,
124+
NumberFormatter::CASH_CURRENCY, and NumberFormatter::CURRENCY_STANDARD.
125+
122126
- XSL:
123127
. The $namespace argument of XSLTProcessor::getParameter(),
124128
XSLTProcessor::setParameter() and XSLTProcessor::removeParameter()

ext/intl/formatter/formatter.stub.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ class NumberFormatter
3131
public const int PATTERN_RULEBASED = UNKNOWN;
3232
/** @cvalue UNUM_IGNORE */
3333
public const int IGNORE = UNKNOWN;
34+
/** @cvalue UNUM_CURRENCY_ISO */
35+
public const int CURRENCY_ISO = UNKNOWN;
36+
/** @cvalue UNUM_CURRENCY_PLURAL */
37+
public const int CURRENCY_PLURAL = UNKNOWN;
3438
/** @cvalue UNUM_CURRENCY_ACCOUNTING */
3539
public const int CURRENCY_ACCOUNTING = UNKNOWN;
40+
/** @cvalue UNUM_CASH_CURRENCY */
41+
public const int CASH_CURRENCY = UNKNOWN;
42+
/** @cvalue UNUM_CURRENCY_STANDARD */
43+
public const int CURRENCY_STANDARD = UNKNOWN;
3644
/** @cvalue UNUM_DEFAULT */
3745
public const int DEFAULT_STYLE = UNKNOWN;
3846

ext/intl/formatter/formatter_arginfo.h

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
--TEST--
2+
NumberFormatter: currency formatting
3+
----DESCRIPTION--
4+
Tests NumberFormatter with various currenct-related formatters.
5+
--EXTENSIONS--
6+
intl
7+
--FILE--
8+
<?php
9+
10+
function ut_main() {
11+
$locales = [
12+
'vi-VN', // VND currency ISO code at the end
13+
'ka-GE',
14+
'is-IS', // ISK: 1 íslensk króna, 0,2+ íslenskar krónur
15+
'zh-TW', // TWD has fractions, but only used in electronic transactions
16+
];
17+
18+
$formats = [
19+
'CURRENCY_ISO' => NumberFormatter::CURRENCY_ISO,
20+
'CURRENCY_PLURAL' => NumberFormatter::CURRENCY_PLURAL,
21+
'CASH_CURRENCY' => NumberFormatter::CASH_CURRENCY,
22+
'CURRENCY_STANDARD' => NumberFormatter::CURRENCY_STANDARD,
23+
];
24+
25+
$numbers = [0, 1, 2, 123456789.42, -123456789.42, 456.789012];
26+
27+
$res_str = '';
28+
29+
foreach($locales as $locale) {
30+
foreach ($formats as $formatLabel => $format) {
31+
$res_str .= "$locale: $formatLabel\n";
32+
foreach ($numbers as $number) {
33+
$fmt = ut_nfmt_create($locale, $format);
34+
$res_str .= "$number => " . ut_nfmt_format_currency($fmt, $number, ut_nfmt_get_symbol($fmt, NumberFormatter::INTL_CURRENCY_SYMBOL)) . "\n";
35+
}
36+
$res_str .= "\n";
37+
}
38+
}
39+
40+
return $res_str;
41+
}
42+
43+
include_once(__DIR__ . '/../ut_common.inc');
44+
45+
ut_run();
46+
?>
47+
--EXPECT--
48+
vi-VN: CURRENCY_ISO
49+
0 => 0 VND
50+
1 => 1 VND
51+
2 => 2 VND
52+
123456789.42 => 123.456.789 VND
53+
-123456789.42 => -123.456.789 VND
54+
456.789012 => 457 VND
55+
56+
vi-VN: CURRENCY_PLURAL
57+
0 => 0 đồng Việt Nam
58+
1 => 1 đồng Việt Nam
59+
2 => 2 đồng Việt Nam
60+
123456789.42 => 123.456.789 đồng Việt Nam
61+
-123456789.42 => -123.456.789 đồng Việt Nam
62+
456.789012 => 457 đồng Việt Nam
63+
64+
vi-VN: CASH_CURRENCY
65+
0 => 0 ₫
66+
1 => 1 ₫
67+
2 => 2 ₫
68+
123456789.42 => 123.456.789 ₫
69+
-123456789.42 => -123.456.789 ₫
70+
456.789012 => 457 ₫
71+
72+
vi-VN: CURRENCY_STANDARD
73+
0 => 0 ₫
74+
1 => 1 ₫
75+
2 => 2 ₫
76+
123456789.42 => 123.456.789 ₫
77+
-123456789.42 => -123.456.789 ₫
78+
456.789012 => 457 ₫
79+
80+
ka-GE: CURRENCY_ISO
81+
0 => 0,00 GEL
82+
1 => 1,00 GEL
83+
2 => 2,00 GEL
84+
123456789.42 => 123 456 789,42 GEL
85+
-123456789.42 => -123 456 789,42 GEL
86+
456.789012 => 456,79 GEL
87+
88+
ka-GE: CURRENCY_PLURAL
89+
0 => 0,00 ქართული ლარი
90+
1 => 1,00 ქართული ლარი
91+
2 => 2,00 ქართული ლარი
92+
123456789.42 => 123 456 789,42 ქართული ლარი
93+
-123456789.42 => -123 456 789,42 ქართული ლარი
94+
456.789012 => 456,79 ქართული ლარი
95+
96+
ka-GE: CASH_CURRENCY
97+
0 => 0,00 ₾
98+
1 => 1,00 ₾
99+
2 => 2,00 ₾
100+
123456789.42 => 123 456 789,42 ₾
101+
-123456789.42 => -123 456 789,42 ₾
102+
456.789012 => 456,79 ₾
103+
104+
ka-GE: CURRENCY_STANDARD
105+
0 => 0,00 ₾
106+
1 => 1,00 ₾
107+
2 => 2,00 ₾
108+
123456789.42 => 123 456 789,42 ₾
109+
-123456789.42 => -123 456 789,42 ₾
110+
456.789012 => 456,79 ₾
111+
112+
is-IS: CURRENCY_ISO
113+
0 => 0 ISK
114+
1 => 1 ISK
115+
2 => 2 ISK
116+
123456789.42 => 123.456.789 ISK
117+
-123456789.42 => -123.456.789 ISK
118+
456.789012 => 457 ISK
119+
120+
is-IS: CURRENCY_PLURAL
121+
0 => 0 íslenskar krónur
122+
1 => 1 íslensk króna
123+
2 => 2 íslenskar krónur
124+
123456789.42 => 123.456.789 íslenskar krónur
125+
-123456789.42 => -123.456.789 íslenskar krónur
126+
456.789012 => 457 íslenskar krónur
127+
128+
is-IS: CASH_CURRENCY
129+
0 => 0 kr.
130+
1 => 1 kr.
131+
2 => 2 kr.
132+
123456789.42 => 123.456.789 kr.
133+
-123456789.42 => -123.456.789 kr.
134+
456.789012 => 457 kr.
135+
136+
is-IS: CURRENCY_STANDARD
137+
0 => 0 kr.
138+
1 => 1 kr.
139+
2 => 2 kr.
140+
123456789.42 => 123.456.789 kr.
141+
-123456789.42 => -123.456.789 kr.
142+
456.789012 => 457 kr.
143+
144+
zh-TW: CURRENCY_ISO
145+
0 => TWD 0.00
146+
1 => TWD 1.00
147+
2 => TWD 2.00
148+
123456789.42 => TWD 123,456,789.42
149+
-123456789.42 => -TWD 123,456,789.42
150+
456.789012 => TWD 456.79
151+
152+
zh-TW: CURRENCY_PLURAL
153+
0 => 0.00 新台幣
154+
1 => 1.00 新台幣
155+
2 => 2.00 新台幣
156+
123456789.42 => 123,456,789.42 新台幣
157+
-123456789.42 => -123,456,789.42 新台幣
158+
456.789012 => 456.79 新台幣
159+
160+
zh-TW: CASH_CURRENCY
161+
0 => $0
162+
1 => $1
163+
2 => $2
164+
123456789.42 => $123,456,789
165+
-123456789.42 => -$123,456,789
166+
456.789012 => $457
167+
168+
zh-TW: CURRENCY_STANDARD
169+
0 => $0.00
170+
1 => $1.00
171+
2 => $2.00
172+
123456789.42 => $123,456,789.42
173+
-123456789.42 => -$123,456,789.42
174+
456.789012 => $456.79

0 commit comments

Comments
 (0)