PHP VERSION: 7.0.20

json_preserve_fractional_part.php

<?php
// jsond vs. json extension
// https://wiki.php.net/rfc/json_preserve_fractional_part

// // Currently
echo json_encode(10.0); // Output 10
echo PHP_EOL;
echo 
json_encode(10.1); // Output 10.1
echo PHP_EOL;
var_dump(json_decode(json_encode(10.0))); // Output int(10)
var_dump(10.0 === json_decode(json_encode(10.0))); // Output bool(false)
echo PHP_EOL;
 
// Proposed
echo json_encode(10.0); // Output 10
echo PHP_EOL;
echo 
json_encode(10.1); // Output 10.1
echo PHP_EOL;
echo 
json_encode(10.0JSON_PRESERVE_ZERO_FRACTION); // Output 10.0
echo PHP_EOL;
echo 
json_encode(10.1JSON_PRESERVE_ZERO_FRACTION); // Output 10.1
echo PHP_EOL;
var_dump(json_decode(json_encode(10.0JSON_PRESERVE_ZERO_FRACTION))); // Output double(10)
var_dump(10.0 === json_decode(json_encode(10.0JSON_PRESERVE_ZERO_FRACTION))); // Output bool(true)
echo PHP_EOL;



Output


10
10.1
int(10)
bool(false)

10
10.1
10.0
10.1
float(10)
bool(true)

SOURCE CODE