8

Probably a simple problem if you know the answer.

What I have

Numbers with no spaces.

$str = 10000;
$str2 = 100000;

What I want

Convert above to below...

$str = '10 000';
$str2 = '100 000';

Own thoughts

I thought of some strpos, regexp, str_replace but is it really the way to solve this? I will accept the most correct, short code answer with no lack of speed.

3
  • 1
    where exactly you want space? Commented Mar 28, 2013 at 8:23
  • 1
    Why is this question downvoted o.O ? Commented Mar 28, 2013 at 8:25
  • 2
    Yes, its silly question, but one can optimize the code with better solution, so plz, its not always to Down-voted such question. Commented Mar 28, 2013 at 8:27

3 Answers 3

17

Use number_format function. With space for thoudsandsSparator as below.

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

Codepad Demo.

Sign up to request clarification or add additional context in comments.

Comments

9

Look no further than number_format.

Comments

4

use number_format:

$formatted = number_format(
    $number, $numberOfDecimals, $decimalSeperator, $thousandsSeperator);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.