@@ -32,31 +32,32 @@ public function encode($value, $depth, array $options, callable $encode)
32
32
return 'NAN ' ;
33
33
} elseif (is_infinite ($ value )) {
34
34
return $ value < 0 ? '-INF ' : 'INF ' ;
35
- } elseif ($ options ['float.integers ' ] && round ($ value ) === $ value ) {
36
- return number_format ($ value , 0 , '. ' , '' );
37
35
}
38
36
39
- return $ this ->enforceType ( $ this -> getFloatString ( $ value , $ options ['float.precision ' ]) );
37
+ return $ this ->getFloat ( $ value , $ options ['float.precision ' ], $ options [ ' float.integers ' ] );
40
38
}
41
39
42
40
/**
43
41
* Converts the float value into string representation.
44
42
* @param float $float Value to convert
45
- * @param integer|false $precision Number of decimals in the number
43
+ * @param integer|false $precision Number of decimals in the number or false for default
44
+ * @param boolean $useIntegers Whether to represent integer values as integers or not
46
45
* @return string The given float value as a string
47
46
*/
48
- private function getFloatString ($ float , $ precision )
47
+ private function getFloat ($ float , $ precision, $ useIntegers )
49
48
{
50
- if ($ precision === false ) {
51
- return (string ) $ float ;
49
+ if ($ useIntegers && round ($ float ) === $ float ) {
50
+ return number_format ($ float , 0 , '. ' , '' );
51
+ } elseif ($ precision === false ) {
52
+ $ output = (string ) $ float ;
53
+ } else {
54
+ $ original = ini_get ('precision ' );
55
+ ini_set ('precision ' , (int ) $ precision );
56
+ $ output = (string ) $ float ;
57
+ ini_set ('precision ' , $ original );
52
58
}
53
59
54
- $ current = ini_get ('precision ' );
55
- ini_set ('precision ' , (int ) $ precision );
56
- $ output = (string ) $ float ;
57
- ini_set ('precision ' , $ current );
58
-
59
- return $ output ;
60
+ return $ this ->enforceType ($ output );
60
61
}
61
62
62
63
/**
0 commit comments