PHP: how to prevent json_encode rounding number automatically?

后端 未结 3 791
暖寄归人
暖寄归人 2021-01-21 12:34

Having a big problem with json_encode it automatically round off the digits but I need 2 decimal points on every digits.

PHP:



        
3条回答
  •  失恋的感觉
    2021-01-21 12:56

    Sounds like you are looking for JSON_PRESERVE_ZERO_FRACTION, available in PHP 5.6.6. Prior versions, you'll need to either convert to string or suck it up and accept that floats and integers are equivalent when there's no fractional value.

    $numbers = [1.00,2.00];
    
    echo $res = json_encode($numbers,JSON_PRESERVE_ZERO_FRACTION);
    

提交回复
热议问题