Reputation:
I have a 'price' variable that contains some integer number from a MySQL database.
I want to check how many numbers the 'price' variable contains, and add a space in the variable depending on how many numbers. See below:
Example:
If 'price' is 150000 I would like the output to be 150 000 (notice the space). OR, if it is 19000 I would like it to output 19 000...
How would you do this the easiest way?
Upvotes: 0
Views: 421
Reputation: 416
If what you are trying to do is format the number with grouped thousands and a space as the thousand separator, use this.
number_format($number, 2, '.', ' ');
Upvotes: 2
Reputation: 655269
Use the number_format
function to format a number:
number_format($number, 0, '.', ' ')
Upvotes: 2
Reputation: 321658
The easiest way is to use number_format()
:
$str = number_format($number, 0, '.', ' ');
Upvotes: 7