-4

PHP number_format not showing up as a number. I should display as number instead of string.

$calc = "500020.66000000";
$calc = number_format((float)$calc, 2, '.', '');
$return_data =
   [
       'data' => [
           'balance' => number_format((float)$calc, 2, '.', ''),
       ],
   ];

return

{
    "data": {
        "balance": "500020.66",
        "currency": "TRY"
    }
}

must be

{
    "data": {
        "balance": 500020.66,
        "currency": "TRY"
    }
}

Fixed:
php.ini add

serialize_precision=-1
round($calc,2);
  • 5
    `number_format` returns a string, because only a string can have *format*. A number only has a *value*, not particular *formatting*. – deceze Aug 24 '22 at 08:22
  • Rather than trying to adjust this misunderstood attempt. Let's focus on what your desired out is. Do you want to round up or down to two decimal places? Do you want a [`bc` function](https://stackoverflow.com/a/41775538/2943403)? – mickmackusa Aug 24 '22 at 09:15

1 Answers1

0

if you take a look into the documentation you will see that number_format returns a string:

 number_format(
    float $num,
    int $decimals = 0,
    ?string $decimal_separator = ".",
    ?string $thousands_separator = ","
): string

BTW: It doesn't make sence to return a formatted number in a json

Jens
  • 67,715
  • 15
  • 98
  • 113