Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

baambaam's avatar

JsonResponse option parameter

In controller i have:

return response()->json(
    [
        'number' => (float)8
    ],
    Response::HTTP_OK,
    [],
    JSON_PRESERVE_ZERO_FRACTION
);

This is output:

{
    "number": 8
}

Is it possible to get 8.0? This working good:

json_encode(['number' => (float)8],JSON_PRESERVE_ZERO_FRACTION);
0 likes
6 replies
Borisu's avatar

Did you try to just write it as 8.0?

json_encode(['number' => (float)8.0],JSON_PRESERVE_ZERO_FRACTION);
baambaam's avatar
json_encode(['number' => (float)8],JSON_PRESERVE_ZERO_FRACTION);

output from this code is:

{"number":8.0}

so this working good. I thinking why laravel version didn't work. Problem is that i get this value from outside api and it's returned as string. I wan't to convert this "5.00005" for float 5.00005 and this "5" to 5.0 . I don't know how many numbers is after dot so i need universal solution. This will be the best but it doesn't work and i don't know why

lostdreamer_nl's avatar
Level 53

It's actually working already, but your browser is removing the .0 from the number.

When I try

return (response()->json(
            [
                'number' => (float)8
            ],
            200,
            [],
            JSON_PRESERVE_ZERO_FRACTION
        ));

In firefox, I see

number: 8

But when I look at the raw source I see:

{"number":8.0}

This is because of the parser in the browser. Here is a bit more info about it on SO https://stackoverflow.com/questions/5520399/how-to-prevent-removing-decimal-point-when-parsing-json

1 like
ganeshr's avatar

return json object, with json float value { "number":8.0}

baambaam's avatar

@lostdreamer_nl yes it looks that it working. when i save my return to $variable and print_r($variable). I get:

Illuminate\Http\JsonResponse Object
(
    [data:protected] => {"number":8.0}
    [callback:protected] => 
    [encodingOptions:protected] => 1024
    [headers] => Symfony\Component\HttpFoundation\ResponseHeaderBag Object
        (
            [computedCacheControl:protected] => Array
                (
                    [no-cache] => 1
                    [private] => 1
                )

            [cookies:protected] => Array
                (
                )

            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [date] => Date
                    [content-type] => Content-Type
                )

            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => no-cache, private
                        )

                    [date] => Array
                        (
                            [0] => Thu, 08 Feb 2018 11:27:34 GMT
                        )

                    [content-type] => Array
                        (
                            [0] => application/json
                        )

                )

            [cacheControl:protected] => Array
                (
                )

        )

    [content:protected] => {"number":8.0}
    [version:protected] => 1.0
    [statusCode:protected] => 200
    [statusText:protected] => OK
    [charset:protected] => 
    [original] => Array
        (
            [number] => 8
        )

    [exception] => 
)

after run curl example.com in terminal i get {"number":8}. So not only in browsers. maybe problem is with this return and Laravel parse my json one more time?

lostdreamer_nl's avatar

no, cURL also uses it's own json parser. The SO question I linked to gives a bit of explanation and a few suggestions about how to write your own JSON parser which will keep the extra decimals.

The easiest way would be to simply cast the float back into a string. Json parsers wont touch it then.

The downside would be that the code processing this json would have to parse the string back into a float.

1 like

Please or to participate in this conversation.