PHP: how to prevent json_encode rounding number automatically?

后端 未结 3 829
暖寄归人
暖寄归人 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
    2021-01-21 12:56

    $numbers = [1.00,2.00];
    

    Here your numbers have already lost their "decimal values". Seriously, try var_dump on them right after this line, you won't get .00 from that either.

    Floating point numbers and integers only contain the numeric value, formatting is not preserved in any way. It's not a json_encode problem, it's a fundamental truth of numeric values in computing.

    If you want to preserve formatting you need to use strings all the way through.

提交回复
热议问题