insert_thousdand_separator()

This function lets you format a number in a way so that it includes a separator every three digits, a "thousand separator".

It is an easy Problem, you want your download list to read "1.305.399 Byte" instead of "1305399 Byte" to give your users a little ease in digit counting. The following function does exatctly this.

Note: The function currently cannot deal with non-Integer numbers (it won't work with 12532,20 $ for instance).

Code

<?php

function insert_thousand_separator ($number)
{
  
$len = strlen ($number);
  if (
$i = $len % 3)
    
$ret = substr($number, 0, $i);
  if (
$len > 3 && $i > 0)
    
$ret = $ret . ".";
  while (
$i < $len)
  {
    
$ret = $ret . substr($number, $i, 3);
    
$i += 3;
    if (
$i < $len)
      
$ret = $ret . ".";
  }  
  return
$ret;
}

?>