Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Last active March 17, 2017 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckreynolds/4a5165ef7307876f2568e7ae2621e719 to your computer and use it in GitHub Desktop.
Save chuckreynolds/4a5165ef7307876f2568e7ae2621e719 to your computer and use it in GitHub Desktop.
<?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