Skip to content

Commit 4388b43

Browse files
committed
When rounding to a precision finer than num, add 0 at the end
1 parent a3d08e5 commit 4388b43

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

ext/bcmath/libbcmath/src/round.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result)
3737
*result = bc_copy_num(BCG(_zero_));
3838
return;
3939
}
40+
/* Just like bcadd('1', '1', 4) becomes '2.0000', it pads with zeros at the end if necessary. */
4041
if (precision >= 0 && num->n_scale <= precision) {
41-
*result = bc_copy_num(num);
42+
if (num->n_scale == precision) {
43+
*result = bc_copy_num(num);
44+
} else if(num->n_scale < precision) {
45+
*result = bc_new_num(num->n_len, precision);
46+
memcpy((*result)->n_value, num->n_value, num->n_len + num->n_scale);
47+
}
4248
return;
4349
}
4450

ext/bcmath/tests/bcround_early_return.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ foreach ($otherModes as $mode) {
7171
--EXPECT--
7272
[123, -4] => 0
7373
[123.123456, -4] => 0
74-
[123, 1] => 123
74+
[123, 1] => 123.0
7575
[123.5, 1] => 123.5
76-
[123.5, 2] => 123.5
76+
[123.5, 2] => 123.50
7777
[123.0000000000000000000001, 22] => 123.0000000000000000000001
78-
[123.0000000000000000000001, 23] => 123.0000000000000000000001
78+
[123.0000000000000000000001, 23] => 123.00000000000000000000010
7979
[-123, -4] => 0
8080
[-123.123456, -4] => 0
81-
[-123, 1] => -123
81+
[-123, 1] => -123.0
8282
[-123.5, 1] => -123.5
83-
[-123.5, 2] => -123.5
83+
[-123.5, 2] => -123.50
8484
[-123.0000000000000000000001, 22] => -123.0000000000000000000001
85-
[-123.0000000000000000000001, 23] => -123.0000000000000000000001
85+
[-123.0000000000000000000001, 23] => -123.00000000000000000000010
8686
[0, 0] => 0
8787
[0.0, 0] => 0
8888
[0.0000, 0] => 0

0 commit comments

Comments
 (0)