php json_decode float loses decimal point
为情所困
为情所困 2017-05-16 13:14:32
0
7
1709
var_dump(json_decode('{"price":5.00}', true));

Result:
array(1) {
["price"]=>
float(5)//But the expectation is 5.00, the decimal point can be retained
}

为情所困
为情所困

reply all(7)
为情所困

It’s not a problem with json_decode, it’s the output function itself,

var_dump(5.00);//输出float(5)
echo 5.00;//输出5
print 5.00;//输出5

json_encode retains decimal points by setting optional options

echo json_encode([5.00], JSON_PRESERVE_ZERO_FRACTION);//输出[5.0],但这也不是期望的[5.00]

echo sprintf('%.2f', 5);//保留两位小数
PHPzhong
var_dump(json_decode('{"price":"5.00"}', true));
迷茫

var_dump is a typed print.

淡淡烟草味

https://www.bytelang.com/o/s/...

Ty80

This is simple, just change 5.00 into string type

小葫芦

This has nothing to do with jeon_encode. It should be formatted during output.

echo number_format($price, 2, '.', '');

or

echo sprintf('%.2f', $price);
左手右手慢动作

var_dump(json_decode('{"price":"5.00"}', true));

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template