Created
December 16, 2017 04:37
-
-
Save puncoz/707cfe35d86fe0a0febe205f8c98b740 to your computer and use it in GitHub Desktop.
Some functions, that help u get readable number. For example: 5145=>5,1K 3465645=>3,4M 4544353454=>4.5B 345,45=>345
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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) : ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment