/**
* Formats the given number for a language/locale. Also used for file size formatting.
*
* @param float|integer $number Number to format
* @param integer|string $precision Number of places to show after decimal point or word "size" for file size
* @param array|null $language Language to override default
*
* @return string Formatted number
*/
public static function numberFormat($number, $precision = 0, array $language = null)
{
if (!$language)
{
$language = self::$_language;
}
if (!$language)
{
$decimalSep = '.';
$thousandsSep = ',';
}
else
{
$decimalSep = $language['decimal_point'];
$thousandsSep = $language['thousands_separator'];
}
if ($precision === 'size')
{
if ($number >= 1099511627776) // 1 TB
{
$number = number_format($number / 1099511627776, 1, $decimalSep, $thousandsSep);
$unit = ' TB';
$phrase = 'x_tb';
}
else if ($number >= 1073741824) // 1 GB
{
$number = number_format($number / 1073741824, 1, $decimalSep, $thousandsSep);
$unit = ' GB';
$phrase = 'x_gb';
}
else if ($number >= 1048576) // 1 MB
{
$number = number_format($number / 1048576, 1, $decimalSep, $thousandsSep);
$unit = ' MB';
$phrase = 'x_mb';
}
else if ($number >= 1024) // 1 KB
{
$number = number_format($number / 1024, 1, $decimalSep, $thousandsSep);
$unit = ' KB';
$phrase = 'x_kb';
}
else
{
$number = number_format($number, 1, $decimalSep, $thousandsSep);
$unit = ' bytes';
$phrase = 'x_bytes';
}
// return $number, not $number.0 when the decimal is 0.
if (substr($number, -2) == $decimalSep . '0')
{
$number = substr($number, 0, -2);
}
if (!$language || !isset($language['phrase_cache'][$phrase]))
{
return $number . $unit;
}
else
{
return str_replace('{size}', $number, $language['phrase_cache'][$phrase]);
}
}
else
{
// return number_format($number, $precision, $decimalSep, $thousandsSep);
$retval = number_format($number, $precision, $decimalSep, $thousandsSep);
$standard = array("0","1","2","3","4","5","6","7","8","9");
$east_arabic = array("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩");
return str_replace($standard , $east_arabic , $retval);
}
}