From 2d696982fe5311294b1c24c8b45f1d051c0f17e2 Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Fri, 6 Dec 2024 16:16:46 +0900 Subject: [PATCH] Now `Number::round()` does not remove trailing zeros --- NEWS | 2 + ext/bcmath/bcmath.c | 2 - .../methods/round_has_trailing_zeros.phpt | 72 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 ext/bcmath/tests/number/methods/round_has_trailing_zeros.phpt diff --git a/NEWS b/NEWS index 19d95ae07cf85..d9568bbae24e2 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,8 @@ PHP NEWS - BcMath: . Fixed bug GH-17049 (Correctly compare 0 and -0). (Saki Takamachi) + . Fixed bug GH-17061 (Now Number::round() does not remove trailing zeros). + (Saki Takamachi) - Iconv: . Fixed bug GH-17047 (UAF on iconv filter failure). (nielsdos) diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 1b4dd331c9b95..832d2758fe968 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -1801,8 +1801,6 @@ PHP_METHOD(BcMath_Number, round) bc_num ret = NULL; bc_round(intern->num, precision, rounding_mode, &ret); - bc_rm_trailing_zeros(ret); - bcmath_number_obj_t *new_intern = bcmath_number_new_obj(ret, ret->n_scale); RETURN_OBJ(&new_intern->std); } diff --git a/ext/bcmath/tests/number/methods/round_has_trailing_zeros.phpt b/ext/bcmath/tests/number/methods/round_has_trailing_zeros.phpt new file mode 100644 index 0000000000000..f526f3b2f91cd --- /dev/null +++ b/ext/bcmath/tests/number/methods/round_has_trailing_zeros.phpt @@ -0,0 +1,72 @@ +--TEST-- +BcMath\Number round() - has trailing zeros +--EXTENSIONS-- +bcmath +--FILE-- +name} ==========\n"; + foreach ([ + ['0.00', 1], + ['0.1', 5], + ['-0.300', 5], + ['1.995', 2], + ] as [$value, $precision]) { + $number = new BcMath\Number($value); + $output = 'value is '; + $output .= str_pad($value, 6, ' ', STR_PAD_RIGHT) . ', '; + $output .= "precision is {$precision}: "; + $output .= $number->round($precision, $mode) . "\n"; + echo $output; + } + echo "\n"; +} +?> +--EXPECT-- +========== HalfAwayFromZero ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 2.00 + +========== HalfTowardsZero ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 1.99 + +========== HalfEven ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 2.00 + +========== HalfOdd ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 1.99 + +========== TowardsZero ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 1.99 + +========== AwayFromZero ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 2.00 + +========== NegativeInfinity ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 1.99 + +========== PositiveInfinity ========== +value is 0.00 , precision is 1: 0.0 +value is 0.1 , precision is 5: 0.10000 +value is -0.300, precision is 5: -0.30000 +value is 1.995 , precision is 2: 2.00