How to convert number to float in PHP

I need to convert both int and float to float, not string.

I tried to convert the number to float with floatval function.

But floatval(int) returns int.

For example:

floatval(100) returns 100

I need to get 100.00

What is the solution?

Answer

You can use number_format function to format the numbers for desired output.

$foo = "105.01";
$zoo = "105";
echo number_format((float)$foo, 2, '.', ''); //Output 105.01
echo number_format((float)$zoo, 2, '.', ''); //Output 105.00

Leave a Reply

Your email address will not be published. Required fields are marked *