xref: /PHP-8.2/ext/filter/tests/031.phpt (revision 74859783)
1--TEST--
2filter_var() and FLOAT
3--EXTENSIONS--
4filter
5--INI--
6precision=14
7--FILE--
8<?php
9
10$floats = array(
11'1.234   ',
12'   1.234',
13'1.234'	,
14'1.2e3',
15'7E3',
16'7E3     ',
17'  7E3     ',
18'  7E-3     '
19);
20
21foreach ($floats as $float) {
22    $out = filter_var($float, FILTER_VALIDATE_FLOAT);
23    var_dump($out);
24}
25
26$floats = array(
27'1.234   '	=> ',',
28'1,234'		=> ',',
29'   1.234'	=> '.',
30'1.234'		=> '..',
31'1.2e3'		=> ','
32);
33
34echo "\ncustom decimal:\n";
35foreach ($floats as $float => $dec) {
36    try {
37        var_dump(filter_var($float, FILTER_VALIDATE_FLOAT, array("options"=>array('decimal' => $dec))));
38    } catch (ValueError $exception) {
39        echo $exception->getMessage() . "\n";
40    }
41}
42
43?>
44--EXPECT--
45float(1.234)
46float(1.234)
47float(1.234)
48float(1200)
49float(7000)
50float(7000)
51float(7000)
52float(0.007)
53
54custom decimal:
55bool(false)
56float(1.234)
57float(1.234)
58filter_var(): "decimal" option must be one character long
59bool(false)
60