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

princeparaste's avatar

number_format() in PHP always returning string instead of float

This might be dumb question, but i need the answer fast, so asking here in the forum.

$num = "39.34343"
echo $num = number_format((float)$num,2, '.', '');
//output $num = "39.34"

When I echo $num always getting string in this case.

So i tried to type cast it with float

echo $num  = (float)number_format((float)$num,2, '.', '')
//output $num = 39.54545454545

I need my value to be 2 decimal float

0 likes
19 replies
tykus's avatar

number_format returns a string; you could instead round to two decimal places:

round($num, 2)
1 like
princeparaste's avatar

Yes i tried with round but its not rounding up my value for some reason. Its giving same result

output $num = 39.54545454545
tykus's avatar

That's what rounding does. So you want to subtract everything after the second decimal?

floor($num * 100) / 100
1 like
princeparaste's avatar

I just want my string value to be converted into 2 decimal float value. The answer you gave does work in normal case when i run in some online php compiler.

But in my case i am return that value as json response, so in the api response i am getting value as 39.5454545454554545455234234545455454545

$response['job'][$key]['cost'] = floor($cost * 100)/100;
return response()->json($response, $status_code);
tykus's avatar

JSON? Use a string representation.

1 like
tykus's avatar
$response['job'][$key]['cost'] = number_format($num, 2); // "39.54"
princeparaste's avatar

Yes, this is excatly what i am doing right now, but the thing is that it gives me string as output $cost = "34.34" when i return it as json response. I want output to float number $cost = 34.34. I guess i got no option other than converting it into float on my frontend.

princeparaste's avatar

hmm ok thnx @tykus . I guess my only option is to convert it into float in my frontend side

princeparaste's avatar

I just need a way to get the 2 decimal float value. I tried with round too but it didn't work. Is there any other method to get it

jlrdw's avatar

Do float first then number_format.

Just example:


            $amt = (float) $array[$i][3];
            $namt = number_format($amt, 2, '.', '');

1 like
princeparaste's avatar

Yes, i tried that but after returning as json response its getting converted into string.

 $namt = number_format((float)$amt, 2, '.', '');
martinbean's avatar

@princeparaste Because you’re using number_format. Which returns a string. Like the PHP docs I linked you to an hour ago says it does.

jlrdw's avatar

Json is string data. Format as needed for display.

Edit:

@princeparaste do you need the number in json without parenthesis? Sorry I mean quotes.

        $num = "39.34343";
        $fnum = (float) $num;
        $znum = round($fnum, 2);
        $namt = [];
        $namt["mynumber"] = $znum;
        $json = json_encode($namt);
        dd($json);

Output

 "{"mynumber":39.34}"

or just json

{
	"mynumber": 39.34
}

Use round like @t... said.

Example here, shorten as necessary. Make an accessor, whatever you need to do to store it.

Snapey's avatar
$num = 39.54545454;

var_dump($num)   //float(39.54545454)

json_encode(['cost' => floor($num*100)/100]);  // {"cost":39.54}

bugsysha's avatar

@princeparaste have you tried?

$num = "39.34343"
echo $num = (float)number_format((float)$num,2, '.', '');
//output $num = 39.34

Please or to participate in this conversation.