BlueChip
BlueChip

Reputation: 774

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?

Upvotes: 0

Views: 2343

Answers (1)

Zam Abdul Vahid
Zam Abdul Vahid

Reputation: 2721

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

Upvotes: 1

Related Questions