2

The PHP function number_format produces weird results when using it with math operations.

Run this...

echo number_format(32545000 - 24343400) . '<br>';
echo number_format(32545000) - number_format(24343400);

Why does the second one produce an answer of "8" instead of the correct answer?

gtilflm
  • 1,389
  • 1
  • 21
  • 51

1 Answers1

5

number_format(32545000) returns a string: 32,545,000

number_format(24343400) returns a string: 24,343,400

The strings are converted to int and you get:

32 - 24 = 8

pNre
  • 5,376
  • 2
  • 22
  • 27
  • Ok. But why wouldn't this work: `echo intval(number_format(32545000)) - intval(number_format(24343400));` – gtilflm Sep 28 '13 at 17:24
  • @gtilflm this code gives exactly the same result. `number_format` formats a number into a print-friendly format. Why would you subtract strings instead of doing `32545000 - 24343400`? – pNre Sep 28 '13 at 17:25