Cyclonecode
Cyclonecode

Reputation: 30021

Number formatting

In my database I have the following numeric values:

1000
1500
10000
12500
100000
890000
1000000
6900000

On the frontend I would like to display these numbers like:

1 000
1 500
10 000
12 500
100 000
890 000
1 000 000
6 900 000

What would be the best way to achive this?

I have tried something like:

function convert_number($number) {
   $string = "".$number."";
   $count = strlen($string);
   $result = '';
   for($i = 0; $i < $count; $i++) {
      $result .= $string[$i];
      if(($i % 4) == 0) $result .= ' ';
   }
   return $result;
 }

Upvotes: 0

Views: 119

Answers (2)

Bhupendra
Bhupendra

Reputation: 258

You can use number_format() function of php

number_format(1000, 0, '', ' ');

this will o/p 1 000

ref: http://php.net/number_format

Upvotes: 0

alizahid
alizahid

Reputation: 969

Use PHP's number_format:

echo number_format( $number, null, null, ' ' );

Upvotes: 5

Related Questions