Readable numbers

Submitted by Camradis - 2 months ago

Some functions, that help u get readable number. For example: 5145=>5,1K 3465645=>3,4M 4544353454=>4.5B 345,45=>345

function readable_number($number, $discharge = 0, $fractional = null, $fractional_count = 1)
{
  $int_number = intval($number);
  
  if($int_number > 999) 
  {
    $num = substr($int_number, 0, strlen($int_number) - 3);
    $discharge++;
    $fractional = substr($int_number, strlen($int_number) - 3);
    
    return readable_number($num, $discharge, $fractional, $fractional_count);
  } 
  
  return $int_number.getFractional($fractional, $fractional_count).getDischargeString($discharge);
}

function getDischargeString($discharge) {
  $discharges = ['K', 'M', 'B','T'];
  return $discharge ? $discharges[$discharge-1] : '';
}

function getFractional($fractional, $fractional_count)
{
  return $fractional ? ','.substr($fractional, 0, $fractional_count) : '';
}
comments powered by Disqus