File: toString.phpt

package info (click to toggle)
php-decimal 1.5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 456 kB
  • sloc: ansic: 1,928; xml: 102; makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,688 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
--TEST--
Decimal::toString
--SKIPIF--
<?php
if (!extension_loaded("decimal")) echo "skip";
?>
--FILE--
<?php
use Decimal\Decimal;

/**
 * Shortcut to construct a new decimal.
 */
function decimal(...$args) { return new Decimal(...$args); }

$tests = [
    [0,             "0"],
    ["0",           "0"],
    ["+0",          "0"],
    ["-0",         "-0"],
    ["0.0",         "0.0"],
    ["1",           "1"],
    ["1.5",         "1.5"],
    ["0.1",         "0.1"],
    ["+0.1",        "0.1"],
    ["-0.1",       "-0.1"],
    [123,           "123"],
    [-123,         "-123"],

    ["0.00000000123",   "1.23E-9"],
    ["0.0000000123",    "1.23E-8"],
    ["0.000000123",     "1.23E-7"],
    ["0.00000123",      "0.00000123"],
    ["0.0000123",       "0.0000123"],
    ["0.000123",        "0.000123"],
    ["0.00123",         "0.00123"],
    ["0.0123",          "0.0123"],
    ["0.123",           "0.123"],
    ["1.23",            "1.23"],
    ["12.3",            "12.3"],
    ["123",             "123"],
    ["1230",            "1230"],
    ["12300",           "12300"],
    ["123000",          "123000"],
    ["1230000",         "1230000"],
    ["12300000",        "12300000"],
    ["123000000",       "123000000"],
    ["1230000000",      "1230000000"],

    // Uppercase E
    ["1.5E20",      "1.5E+20"],
    ["1.5E+20",     "1.5E+20"],
    ["1.5E-20",     "1.5E-20"],

    ["+1.5E20",     "1.5E+20"],
    ["+1.5E+20",    "1.5E+20"],
    ["+1.5E-20",    "1.5E-20"],

    ["-1.5E20",     "-1.5E+20"],
    ["-1.5E+20",    "-1.5E+20"],
    ["-1.5E-20",    "-1.5E-20"],

    // Lowercase e
    ["1.5e20",      "1.5E+20"],
    ["1.5e+20",     "1.5E+20"],
    ["1.5e-20",     "1.5E-20"],

    ["+1.5e20",     "1.5E+20"],
    ["+1.5e+20",    "1.5E+20"],
    ["+1.5e-20",    "1.5E-20"],

    ["-1.5e20",     "-1.5E+20"],
    ["-1.5e+20",    "-1.5E+20"],
    ["-1.5e-20",    "-1.5E-20"],

    // Special numbers
    [ "NAN",        "NAN"],
    [ "INF",        "INF"],
    ["+INF",        "INF"],
    ["-INF",       "-INF"],
    [ "nan",        "NAN"],
    [ "inf",        "INF"],
    ["+inf",        "INF"],
    ["-inf",       "-INF"],
    [ "NaN",        "NAN"],
    [ "Inf",        "INF"],
    ["+Inf",        "INF"],
    ["-Inf",       "-INF"],

    // Nested
    [decimal("0.1"), "0.1"],
];

foreach ($tests as $test) {
    $number = $test[0];
    $expect = $test[1];

    $results = [
        (string) decimal($number),
        decimal($number)->toString(),
        decimal($number)->__toString(),
    ];

    foreach ($results as $result) {
        if ($result !== $expect) {
            print_r(compact("number", "result", "expect"));
            break;
        }
    }
}

?>
--EXPECT--