-3

I want to format a double value returned from db to 2 decimal places

I tried number_format($amount,2) but it didn't work

  • What does "didn't work" mean? With some test code, like `$amount = (double)2;` followed by `number_format($amount, 2)`, I get `"2.00"`, which looks correct to me... What is happening in your code? – Tim Lewis Apr 24 '23 at 16:56
  • "Didn't work" is not an adequate description of the issue. What was the result? What did you expect? – Sammitch Apr 24 '23 at 19:50

2 Answers2

0

Try and explicitly cast the double value to float like mentioned in this thread here

Also, check what type is the $amount variable in your example..

0

Use the php number_format

number_format($number, 2, '.', ',');

  1. 2 decimal places.
  2. '.' for decimal separator
  3. ',' for thousand separator
It's VIN
  • 152
  • 7