0

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?

xorozo
  • 540
  • 4
  • 18
  • 4
    If you want the .00, this is more about formatting than storage internally of the value. – Nigel Ren Mar 12 '21 at 18:54
  • @NigelRen. Thanks for your comment. As you said, If this problem is related to formatting, it returns a string. it's not `float`. if I convert string to float, it returns int or float. What do you think about that? – xorozo Mar 12 '21 at 18:59
  • 2
    `number_format` would be a good function to look at for formatting – hppycoder Mar 12 '21 at 19:00
  • @hppycoder. Good answer. thanks. but not perfect. Android requires the exact data type. `number_format` returns `string`, it is not `float` – xorozo Mar 12 '21 at 19:05
  • Are you asking why the language doesn't natively hold the decimal places on an integer? – hppycoder Mar 12 '21 at 19:08
  • More information at https://stackoverflow.com/questions/4483540/show-a-number-to-two-decimal-places – Nigel Ren Mar 12 '21 at 19:08
  • I would wager that you are misunderstand how values are passed over HTTP to your Android app, and the handling within that app is at fault, not anything particularly to do with PHP. You should post a new question with code from both sides, and we might be able to give you more pertinent advice. – Sammitch Mar 12 '21 at 20:34

1 Answers1

1

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
Zam Abdul Vahid
  • 2,389
  • 3
  • 18
  • 25