Skip to content

Commit a4ff76e

Browse files
committed
ext/intl: Preserve int64 precision in IntlNumberRangeFormatter::format()
1 parent 425cd3d commit a4ff76e

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

ext/intl/rangeformatter/rangeformatter_class.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ zend_object *IntlNumberRangeFormatter_object_create(zend_class_entry *ce)
6060
return &intern->zo;
6161
}
6262

63+
static icu::Formattable rangeformatter_create_formattable(zval *number)
64+
{
65+
icu::Formattable formattable;
66+
67+
if (Z_TYPE_P(number) == IS_DOUBLE) {
68+
formattable.setDouble(Z_DVAL_P(number));
69+
} else {
70+
formattable.setInt64(static_cast<int64_t>(Z_LVAL_P(number)));
71+
}
72+
73+
return formattable;
74+
}
75+
6376
U_CFUNC PHP_METHOD(IntlNumberRangeFormatter, __construct)
6477
{
6578
ZEND_PARSE_PARAMETERS_NONE();
@@ -154,8 +167,8 @@ U_CFUNC PHP_METHOD(IntlNumberRangeFormatter, format)
154167

155168
UErrorCode error = U_ZERO_ERROR;
156169

157-
icu::Formattable start_formattable(Z_TYPE_P(start) == IS_DOUBLE ? Z_DVAL_P(start) : Z_LVAL_P(start));
158-
icu::Formattable end_formattable(Z_TYPE_P(end) == IS_DOUBLE ? Z_DVAL_P(end) : Z_LVAL_P(end));
170+
icu::Formattable start_formattable = rangeformatter_create_formattable(start);
171+
icu::Formattable end_formattable = rangeformatter_create_formattable(end);
159172

160173
UnicodeString result = RANGEFORMATTER_OBJECT(obj)->formatFormattableRange(start_formattable, end_formattable, error).toString(error);
161174

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
IntlNumberRangeFormatter::format() preserves int64 precision
3+
--EXTENSIONS--
4+
intl
5+
--SKIPIF--
6+
<?php
7+
if (!class_exists('IntlNumberRangeFormatter')) {
8+
die('skip IntlNumberRangeFormatter not available');
9+
}
10+
if (PHP_INT_SIZE < 8) {
11+
die('skip 64-bit only');
12+
}
13+
?>
14+
--FILE--
15+
<?php
16+
$formatter = IntlNumberRangeFormatter::createFromSkeleton(
17+
'',
18+
'en_US',
19+
IntlNumberRangeFormatter::COLLAPSE_AUTO,
20+
IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE
21+
);
22+
23+
$formatted = $formatter->format(9007199254740993, 9007199254740993);
24+
var_dump(preg_replace('/[^0-9]/', '', $formatted));
25+
?>
26+
--EXPECT--
27+
string(16) "9007199254740993"

0 commit comments

Comments
 (0)