Last active
March 17, 2017 21:13
-
-
Save chuckreynolds/4a5165ef7307876f2568e7ae2621e719 to your computer and use it in GitHub Desktop.
This file contains 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 formatMoney( int $number = null ) { | |
// display numbers as K or B or M | |
if ($number < 1000) { | |
// Anything less than a thousand | |
$format_num = number_format($number); | |
} else if ($number < 1000000) { | |
// Anything less than a million | |
$format_num = number_format($number / 1000, 0) . 'K'; | |
} else if ($number < 1000000000) { | |
// Anything less than a billion | |
$format_num = number_format($number / 1000000, 1) . 'M'; | |
} else { | |
// At least a billion | |
$format_num = number_format($number / 1000000000, 1) . 'B'; | |
} | |
return $format_num; | |
} | |
$number = 15057450000; | |
if ($number) { | |
echo formatMoney($number); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment