1--TEST--
2bcround() function with early return
3--EXTENSIONS--
4bcmath
5--FILE--
6<?php
7
8$early_return_cases = [
9    ['123', -4],
10    ['123.123456', -4],
11    ['123', 1],
12    ['123.5', 1],
13    ['123.5', 2],
14    ['123.0000000000000000000001', 22],
15    ['123.0000000000000000000001', 23],
16    ['-123', -4],
17    ['-123.123456', -4],
18    ['-123', 1],
19    ['-123.5', 1],
20    ['-123.5', 2],
21    ['-123.0000000000000000000001', 22],
22    ['-123.0000000000000000000001', 23],
23    ['0', 0],
24    ['0.0', 0],
25    ['0.0000', 0],
26    ['-0', 0],
27    ['-0.0', 0],
28    ['-0.0000', 0],
29];
30
31$results = [
32    RoundingMode::HalfAwayFromZero->name => [],
33];
34foreach ($early_return_cases as [$num, $precision]) {
35    $result = str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT) . ' => ' . bcround($num, $precision, RoundingMode::HalfAwayFromZero) . "\n";
36    echo $result;
37    $results[RoundingMode::HalfAwayFromZero->name][] = $result;
38}
39
40echo "\n";
41
42foreach (RoundingMode::cases() as $mode) {
43    $results[$mode->name] = [];
44    foreach ($early_return_cases as [$num, $precision]) {
45        $result = str_pad("[{$num}, {$precision}]", 33, ' ', STR_PAD_LEFT) . ' => ' . bcround($num, $precision, $mode) . "\n";
46        $results[$mode->name][] = $result;
47    }
48
49    if ($results[RoundingMode::HalfAwayFromZero->name] === $results[$mode->name]) {
50        echo str_pad($mode->name, 24, ' ', STR_PAD_LEFT) . ": result is same to HalfAwayFromZero\n";
51    } else {
52        echo str_pad($mode->name, 24, ' ', STR_PAD_LEFT) . ": result is not same to HalfAwayFromZero, failed\n";
53    }
54}
55?>
56--EXPECT--
57                        [123, -4] => 0
58                 [123.123456, -4] => 0
59                         [123, 1] => 123.0
60                       [123.5, 1] => 123.5
61                       [123.5, 2] => 123.50
62 [123.0000000000000000000001, 22] => 123.0000000000000000000001
63 [123.0000000000000000000001, 23] => 123.00000000000000000000010
64                       [-123, -4] => 0
65                [-123.123456, -4] => 0
66                        [-123, 1] => -123.0
67                      [-123.5, 1] => -123.5
68                      [-123.5, 2] => -123.50
69[-123.0000000000000000000001, 22] => -123.0000000000000000000001
70[-123.0000000000000000000001, 23] => -123.00000000000000000000010
71                           [0, 0] => 0
72                         [0.0, 0] => 0
73                      [0.0000, 0] => 0
74                          [-0, 0] => 0
75                        [-0.0, 0] => 0
76                     [-0.0000, 0] => 0
77
78        HalfAwayFromZero: result is same to HalfAwayFromZero
79         HalfTowardsZero: result is same to HalfAwayFromZero
80                HalfEven: result is same to HalfAwayFromZero
81                 HalfOdd: result is same to HalfAwayFromZero
82             TowardsZero: result is same to HalfAwayFromZero
83            AwayFromZero: result is same to HalfAwayFromZero
84        NegativeInfinity: result is same to HalfAwayFromZero
85        PositiveInfinity: result is same to HalfAwayFromZero
86