Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Jun 8, 2017
1 parent 181a30f commit aaade59
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,36 @@ function slug_trans($word)
{
return Phphub\Handler\SlugTranslate::translate($word);
}

// Shortens a number and attaches K, M, B, etc. accordingly
function number_shorten($number, $precision = 1, $divisors = null) {

if ($number < 1000) {
return $number;
}
// Setup default $divisors if not provided
if (!isset($divisors)) {
$divisors = array(
pow(1000, 0) => '', // 1000^0 == 1
pow(1000, 1) => 'k', // Thousand
pow(1000, 2) => 'm', // Million
pow(1000, 3) => 'b', // Billion
pow(1000, 4) => 't', // Trillion
pow(1000, 5) => 'Qa', // Quadrillion
pow(1000, 6) => 'Qi', // Quintillion
);
}

// Loop through each $divisor and find the
// lowest amount that matches
foreach ($divisors as $divisor => $shorthand) {
if (abs($number) < ($divisor * 1000)) {
// We found a match!
break;
}
}

// We found our match, or there were no matches.
// Either way, use the last defined value for $divisor.
return number_format($number / $divisor, $precision) . $shorthand;
}
2 changes: 1 addition & 1 deletion resources/views/topics/partials/topics.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<span class="count_seperator">/</span>

<span class="count_of_visits" title="查看数">
{{ $topic->view_count }}
{{ number_shorten($topic->view_count) }}
</span>
<span class="count_seperator">|</span>

Expand Down

0 comments on commit aaade59

Please sign in to comment.