PHP: how to prevent json_encode rounding number automatically?

后端 未结 3 794
暖寄归人
暖寄归人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 12:46

    You can use your code in this way as follows:

     $number) 
     {
        $numbers[$i] = number_format($number, 2, '.', null);
     }
    
      echo $res = json_encode($numbers); // ["1.00","2.00"]
      echo str_replace('"', '', $res); //[1.00,2.00]
     ?>
    

    and Your EXPECTED OUTPUT: [1.00,2.00] which is same.

提交回复
热议问题