user188962
user188962

Reputation:

How to check how many characters in variable, and add space between characters in that var?

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

Answers (3)

Kailash Badu
Kailash Badu

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

Gumbo
Gumbo

Reputation: 655269

Use the number_format function to format a number:

number_format($number, 0, '.', ' ')

Upvotes: 2

Greg
Greg

Reputation: 321658

The easiest way is to use number_format():

$str = number_format($number, 0, '.', ' ');

Upvotes: 7

Related Questions