Skip to content

Commit c20b429

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 2d39c78 commit c20b429

File tree

3 files changed

+224
-1
lines changed

3 files changed

+224
-1
lines changed

ext/intl/formatter/formatter.stub.php

+8
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

+25-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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+
'ka-GE',
13+
'hi-IN', // No spaces between sign and the amount, crore number segmenting
14+
'zh-TW', // TWD has fractions, but only used in electronic transactions
15+
];
16+
17+
$formats = [
18+
'CURRENCY' => NumberFormatter::CURRENCY,
19+
'CURRENCY_ACCOUNTING' => NumberFormatter::CURRENCY_ACCOUNTING,
20+
'CURRENCY_ISO' => NumberFormatter::CURRENCY_ISO,
21+
'CURRENCY_PLURAL' => NumberFormatter::CURRENCY_PLURAL,
22+
'CASH_CURRENCY' => NumberFormatter::CASH_CURRENCY,
23+
'CURRENCY_STANDARD' => NumberFormatter::CURRENCY_STANDARD,
24+
];
25+
26+
$numbers = [0, 1, 2, 123456789.42, -123456789.42, 456.789012];
27+
28+
$res_str = '';
29+
30+
foreach($locales as $locale) {
31+
foreach ($formats as $formatLabel => $format) {
32+
$res_str .= "$locale: $formatLabel\n";
33+
foreach ($numbers as $number) {
34+
$fmt = ut_nfmt_create($locale, $format);
35+
$res_str .= "$number => " . ut_nfmt_format_currency($fmt, $number, ut_nfmt_get_symbol($fmt, NumberFormatter::INTL_CURRENCY_SYMBOL)) . "\n";
36+
}
37+
$res_str .= "\n";
38+
}
39+
}
40+
41+
return $res_str;
42+
}
43+
44+
include_once(__DIR__ . '/../ut_common.inc');
45+
46+
ut_run();
47+
?>
48+
--EXPECT--
49+
ka-GE: CURRENCY
50+
0 => 0,00 ₾
51+
1 => 1,00 ₾
52+
2 => 2,00 ₾
53+
123456789.42 => 123 456 789,42 ₾
54+
-123456789.42 => -123 456 789,42 ₾
55+
456.789012 => 456,79 ₾
56+
57+
ka-GE: CURRENCY_ACCOUNTING
58+
0 => 0,00 ₾
59+
1 => 1,00 ₾
60+
2 => 2,00 ₾
61+
123456789.42 => 123 456 789,42 ₾
62+
-123456789.42 => -123 456 789,42 ₾
63+
456.789012 => 456,79 ₾
64+
65+
ka-GE: CURRENCY_ISO
66+
0 => 0,00 GEL
67+
1 => 1,00 GEL
68+
2 => 2,00 GEL
69+
123456789.42 => 123 456 789,42 GEL
70+
-123456789.42 => -123 456 789,42 GEL
71+
456.789012 => 456,79 GEL
72+
73+
ka-GE: CURRENCY_PLURAL
74+
0 => 0,00 ქართული ლარი
75+
1 => 1,00 ქართული ლარი
76+
2 => 2,00 ქართული ლარი
77+
123456789.42 => 123 456 789,42 ქართული ლარი
78+
-123456789.42 => -123 456 789,42 ქართული ლარი
79+
456.789012 => 456,79 ქართული ლარი
80+
81+
ka-GE: CASH_CURRENCY
82+
0 => 0,00 ₾
83+
1 => 1,00 ₾
84+
2 => 2,00 ₾
85+
123456789.42 => 123 456 789,42 ₾
86+
-123456789.42 => -123 456 789,42 ₾
87+
456.789012 => 456,79 ₾
88+
89+
ka-GE: CURRENCY_STANDARD
90+
0 => 0,00 ₾
91+
1 => 1,00 ₾
92+
2 => 2,00 ₾
93+
123456789.42 => 123 456 789,42 ₾
94+
-123456789.42 => -123 456 789,42 ₾
95+
456.789012 => 456,79 ₾
96+
97+
hi-IN: CURRENCY
98+
0 => ₹0.00
99+
1 => ₹1.00
100+
2 => ₹2.00
101+
123456789.42 => ₹12,34,56,789.42
102+
-123456789.42 => -₹12,34,56,789.42
103+
456.789012 => ₹456.79
104+
105+
hi-IN: CURRENCY_ACCOUNTING
106+
0 => ₹0.00
107+
1 => ₹1.00
108+
2 => ₹2.00
109+
123456789.42 => ₹12,34,56,789.42
110+
-123456789.42 => -₹12,34,56,789.42
111+
456.789012 => ₹456.79
112+
113+
hi-IN: CURRENCY_ISO
114+
0 => INR 0.00
115+
1 => INR 1.00
116+
2 => INR 2.00
117+
123456789.42 => INR 12,34,56,789.42
118+
-123456789.42 => -INR 12,34,56,789.42
119+
456.789012 => INR 456.79
120+
121+
hi-IN: CURRENCY_PLURAL
122+
0 => 0.00 भारतीय रुपया
123+
1 => 1.00 भारतीय रुपया
124+
2 => 2.00 भारतीय रुपए
125+
123456789.42 => 12,34,56,789.42 भारतीय रुपए
126+
-123456789.42 => -12,34,56,789.42 भारतीय रुपए
127+
456.789012 => 456.79 भारतीय रुपए
128+
129+
hi-IN: CASH_CURRENCY
130+
0 => ₹0.00
131+
1 => ₹1.00
132+
2 => ₹2.00
133+
123456789.42 => ₹12,34,56,789.42
134+
-123456789.42 => -₹12,34,56,789.42
135+
456.789012 => ₹456.79
136+
137+
hi-IN: CURRENCY_STANDARD
138+
0 => ₹0.00
139+
1 => ₹1.00
140+
2 => ₹2.00
141+
123456789.42 => ₹12,34,56,789.42
142+
-123456789.42 => -₹12,34,56,789.42
143+
456.789012 => ₹456.79
144+
145+
zh-TW: CURRENCY
146+
0 => $0.00
147+
1 => $1.00
148+
2 => $2.00
149+
123456789.42 => $123,456,789.42
150+
-123456789.42 => -$123,456,789.42
151+
456.789012 => $456.79
152+
153+
zh-TW: CURRENCY_ACCOUNTING
154+
0 => $0.00
155+
1 => $1.00
156+
2 => $2.00
157+
123456789.42 => $123,456,789.42
158+
-123456789.42 => ($123,456,789.42)
159+
456.789012 => $456.79
160+
161+
zh-TW: CURRENCY_ISO
162+
0 => TWD 0.00
163+
1 => TWD 1.00
164+
2 => TWD 2.00
165+
123456789.42 => TWD 123,456,789.42
166+
-123456789.42 => -TWD 123,456,789.42
167+
456.789012 => TWD 456.79
168+
169+
zh-TW: CURRENCY_PLURAL
170+
0 => 0.00 新台幣
171+
1 => 1.00 新台幣
172+
2 => 2.00 新台幣
173+
123456789.42 => 123,456,789.42 新台幣
174+
-123456789.42 => -123,456,789.42 新台幣
175+
456.789012 => 456.79 新台幣
176+
177+
zh-TW: CASH_CURRENCY
178+
0 => $0
179+
1 => $1
180+
2 => $2
181+
123456789.42 => $123,456,789
182+
-123456789.42 => -$123,456,789
183+
456.789012 => $457
184+
185+
zh-TW: CURRENCY_STANDARD
186+
0 => $0.00
187+
1 => $1.00
188+
2 => $2.00
189+
123456789.42 => $123,456,789.42
190+
-123456789.42 => -$123,456,789.42
191+
456.789012 => $456.79

0 commit comments

Comments
 (0)