I have the following php I use in Wordpress on the frontend to display how many total comments my blog has. So say for example would render to display something to this effect: 1,265,788
<?php
$comments_count = wp_count_comments();
echo number_format($comments_count->total_comments);
?>
What I would like to do is format the number better. So instead of it saying I have 1,265,788 comments, it will say I have 1,265M comments.
I tried the following code as recommended by another post, but does not work either. It echo's the full number.
<?php
$comments_count = wp_count_comments();
if ($comments_count->total_comments < 1000000) {
// Anything less than a million
$n_format = number_format($comments_count->total_comments);
echo $n_format;
} else if ($comments_count->total_comments < 1000000000) {
// Anything less than a billion
$n_format = number_format($comments_count->total_comments / 1000000, 3) . 'M';
echo $n_format;
} else {
// At least a billion
$n_format = number_format($comments_count->total_comments / 1000000000, 3) . 'B';
echo $n_format;
}
?>
So no, this is not a duplicate question. Previous answers are of absolute no help to me. I tried exactly like the answers said and the output I get is the full number like the original top code gives me.
Anyone have an idea how I can achieve this and can show me a sample please.
Thank you!
Actually above code is working fine and output is 1.266M
Hard coded example:
$number = 1265788;
if ($number < 1000000) {
// Anything less than a million
$n_format = number_format($number);
echo $n_format;
} else if ($number < 1000000000) {
// Anything less than a billion
$n_format = number_format($number / 1000000, 3) . 'M';
echo $n_format;
} else {
// At least a billion
$n_format = number_format($number / 1000000000, 3) . 'B';
echo $n_format;
}
Dynamic:
$comments_count = wp_count_comments();
$number = $comments_count->total_comments;
if ($number < 1000000) {
// Anything less than a million
$n_format = number_format($number);
echo $n_format;
} else if ($number < 1000000000) {
// Anything less than a billion
$n_format = number_format($number / 1000000, 3) . 'M';
echo $n_format;
} else {
// At least a billion
$n_format = number_format($number / 1000000000, 3) . 'B';
echo $n_format;
}
Related
I am creating a social site. And I want to show people things like their total amount of likes, followers and people they are following. The way it is now, it shows the total amount of likes, followers and following as a whole number and if it's too long it will go over other words on the page.
So how do I use abbreviations like: K(for thousands), m(millions) etc ? This is what I have now.
$stmt = $con->prepare('SELECT name, username, num_likes, profile_pic FROM users WHERE user_closed = "0"
ORDER BY num_likes DESC LIMIT 100');
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($name, $username, $num_likes, $profile_pic);
function convert($num_likes)
{
$num_likes = $number / 1000;
return $num_likes . 'k';
}
This is how I show the result: <p> Total Likes: " . $num_likes ."</p>
I tried the following:
PHP Count round thousand to a K style count like facebook Share . . . Twitter Button ect
Shorten long numbers to K/M/B?
PHP Count round thousand to a K style count Facebook Share
First of all, your function:
function convert($num_likes)
{
$num_likes = $number / 1000;
return $num_likes . 'k';
}
will not work as expected, because it converts to the opposite way :) Here is updated version:
function convert($num_likes)
{
$number = $num_likes / 1000;
return $number . 'k';
}
Second point. You should use the function somewhere... for example your line (actually only a part of it):
<p> Total Likes: " . $num_likes ."</p>
must be:
<p> Total Likes: " . convert($num_likes) ."</p>
And finally, using this answer we can modify convert function to this:
function convert($n) {
if ($n < 1000) {
$n_format = number_format($n);
} else if ($n < 1000000) {
// Anything less than a million
$n_format = number_format($n / 1000, 3) . 'k';
} else if ($n < 1000000000) {
// Anything less than a billion
$n_format = number_format($n / 1000000, 3) . 'M';
} else {
// At least a billion
$n_format = number_format($n / 1000000000, 3) . 'B';
}
return $n_format;
}
Now we can convert all numbers up to billions.
Playground: click.
Perhaps like this,
Use round() if you don't want large fractions.
<?php
function convert(int $number)
{
if ($number >= 1E9) {
return round($number / 1E9, 2).'b';
} else if ($number >= 1E6) {
return round($number / 1E6, 2).'m';
} else if ($number >= 1E3) {
return round($number / 1E3, 2).'k';
}
return $number;
}
echo convert(1000000000).PHP_EOL; // 1b
echo convert(1000000).PHP_EOL; // 1m
echo convert(1200).PHP_EOL; // 1.2k
echo convert(1234).PHP_EOL; // 1.23k
echo convert(100).PHP_EOL; // 100
https://3v4l.org/cc54H
I'm trying to round a number to 1 decimal place.
I've written this code to shorten numbers over 1000:
$numbers = array($count);
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k";
}
}
foreach($numbers as $number) {
echo "Posts: ";
echo format_number($number);
}
For example this makes 15900 15.900k. Now I made a rounding part so it only reads as 15.9k:
$rounded = round($number, 1); // e.g. 66.7346 becomes 66.7
and echoed it:
echo "".$rounded."";
but nothing shows.
Any ideas?
You aren't returning anything if the number is less than 1000.
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k";
}
//What happens here if the number is not 1000?
}
I'd rewrite it as this:
function format_number($number) {
$append = '';
if($number >= 1000) {
$number /= 1000;
$append = 'k';
}
return round($number, 1) . $append;
}
I have a value as a number. For instance, 502.
I want to write a php if statement that will display some text if the value is lesser or greater than certain numbers, or between a range.
E.g.
number is 502, text will say: "Between 500-600"
number is 56, text will say: "Between 0-60"
etc.
So far I have this:
<?php $count=0;?>
<?php $board = getUserBoard($userDetails['userId']);?>
<?php if(is_array($board)):?>
<?php $boardCount = count($board);?>
<?php foreach($board as $key=>$value):?>
<?php
$boardPin = getEachBoardPins($value->id);
$count = $count + count($boardPin);
?>
<?php endforeach?>
<?php endif?>
And that gives me a number:
<?php echo $count;?>
I have tried writing...
<?php if(($count)): => 500 ?>
Over 500
<?php endif ?>
But I keep running into errors.
I'd like to create a list if possible with elseif statements denoting various number ranges.
E.g.
0-50, 51-250, 251-500 etc.
Can anyone help me?
Thanks.
The sanest, neatest and most widely used syntax for if conditions in PHP is:
if($value >=500 && $value <=600 )
{
echo "value is between 500 and 600";
}
if ($count >= 0 && $count < 100) {
echo 'between 0 et 99';
} elseif ($count < 199) {
echo 'between 100 and 199';
} elseif { ...
}elseif ($count < 599) {
echo 'between 500 and 599';
} else {
echo 'greater or equal than 600';
}
I wrote something like this a few years back (might be a better way to do it):
function create_range($p_num, $p_group = 1000) {
$i = 0;
while($p_num >= $i) {
$i += $p_group;
}
$i -= $p_group;
return $i . '-' . ($i + $p_group - 1);
}
print 'The number is between ' . create_range(502, 100) . '.';
It'll say 500-599, but you can adjust it to your needs.
I'm not sure what you need, but here is what I understand you ask:
function getRange($n, $limit = array(50, 250, 500)) { // Will create the ranges 0-50, 51-250, 251-500 and 500-infinity
$previousLimit = 0;
foreach ($limits as $limit) {
if ($n < $limit) {
return 'Between ' . ($previousLimit + 1) . ' and ' . $limit; //Return whatever you need.
}
$previousLimit = $limit;
}
return 'Greater than ' . $previousLimit; // Return whatever you need.
}
echo getRange(56); // Prints "Between 51 and 250"
echo getRange(501); // Prints "Greater than 500"
echo getRange(12, array(5, 10, 15, 20)); // Prints "Between 11 and 15"
function getRange($number){
$length=strlen($number);
$length--;
$r1=round($number,-$length);
if ($r1>$number){
$r2=$r1-pow(10,$length);
return ''.$number.' value is between '.$r2.'-'.$r1;
}
else {
$r2=$r1+pow(10,$length);
return ''.$number.' value is between '.$r1.'-'.$r2;
}
}
Try this.
I need to round up any integer between 1 and infinity in php to the next significant figure (though in practice I'm unlikely to need to round up infinity, so will be happy to settle on reasonable internal limits) eg:
$x <= 10 ? $x = 10
10 < $x <= 100 ? $x = 100
100 < $x <= 1000 ? $x = 1000
etc.
Round / ceil etc don't seem to do the job quite as planned. A pointer towards the correct algorhythm (or function?) would be much appreciated
i think this method will fix your problem:
function n($nr, $p = 10) {
if($nr <= $p) {
return $p;
}
return n($nr, $p*10);
}
heres the result:
echo n(1);
//output 10
echo n(232);
//output 1000
echo n(89289382);
//output 100000000
$x = pow(10,floor(log10($x)) + (floor(log10($x)) == log10($x) && $x!=1 ? 0:1) );
function my_ceil($in) {
if($in == 1) return $in;
if($in == pow(10, strlen($in)-1)) return $in;
return pow(10, strlen($in));
}
echo my_ceil(11); //100
echo my_ceil(10); //10
I think this is what you're looking for:
echo ceil($x / pow(10, strlen($x))) * pow(10, strlen($x));
Only works when $x is an integer, but you say in your question that that is indeed the case, so there's no issue (unless you try to later use it with numbers containing decimals).
This should do the trick:
<?php
function nextSignificantFeature($number){
$upper = pow(10, strlen($number));
return $number == $upper/10 ? $number : $upper;
}
?>
Actually there is the infinity number in PHP, so the implementation should deal with it as you wrote any number from 1 up to infinity Demo:
<?php
function n($number) {
if ($number < 1) {
throw new InvalidArgumentException('Number must be greater or equal 1.');
}
if ($number === INF) {
return INF;
}
$p = 10;
while($number > ($p*=10));
return $p;
}
echo n(1), "\n";
//output 10
echo n(232), "\n";
//output 1000
echo n(89289382), "\n";
//output 100000000
echo n(INF), "\n";
// output INF
echo n(-INF), "\n";
// throws exception 'InvalidArgumentException' with message 'Number must be greater or equal 1.'
This example does the iterative calculation in PHP userland code. There are some math functions in PHP that can do it inline like pow.
Re,
One photo with exposure being 1/640 has the EXIF field of "ExposureTime" eq. "15625/10000000". I am not sure why some photos display this value in a readable format (e.g., "1/100"), but I need to convert this "15625" back to "1/640". How? :)
Thanks.
It's simple mathematics: simply divide the top and bottom of the fraction by the top value.
15625 / 10000000
= (15625/15625) / (10000000/15625)
= 1 / 640
In PHP, you can do it like this:
$exposure = "15625/10000000";
$parts = explode("/", $exposure);
$exposure = implode("/", array(1, $parts[1]/$parts[0]));
echo $exposure;
I improved upon ZZ Coders implementation with a few sanity checks and special cases. It seems to work well with my images with several special cases thrown at it. Please let me know if there are any issues and we'll improve it.
// Exposure Time
$exif = exif_read_data($fullPath, 'IFD0', true);
$arrExposureTime = explode('/', $exif['EXIF']['ExposureTime']);
// Sanity check for zero denominator.
if ($arrExposureTime[1] == 0) {
$ExposureTime = '<sup>1</sup>/? sec';
// In case numerator is zero.
} elseif ($arrExposureTime[0] == 0) {
$ExposureTime = '<sup>0</sup>/' . $arrExposureTime[1] . ' sec';
// When denominator is 1, display time in whole seconds, minutes, and/or hours.
} elseif ($arrExposureTime[1] == 1) {
// In the Seconds range.
if ($arrExposureTime[0] < 60) {
$ExposureTime = $arrExposureTime[0] . ' s';
// In the Minutes range.
} elseif (($arrExposureTime[0] >= 60) && ($arrExposureTime[0] < 3600)) {
$ExposureTime = gmdate("i\m:s\s", $arrExposureTime[0]);
// In the Hours range.
} else {
$ExposureTime = gmdate("H\h:i\m:s\s", $arrExposureTime[0]);
}
// When inverse is evenly divisable, show reduced fractional exposure.
} elseif (($arrExposureTime[1] % $arrExposureTime[0]) == 0) {
$ExposureTime = '<sup>1</sup>/' . $arrExposureTime[1]/$arrExposureTime[0] . ' sec';
// If the value is greater or equal to 3/10, which is the smallest standard
// exposure value that doesn't divid evenly, show it in decimal form.
} elseif (($arrExposureTime[0]/$arrExposureTime[1]) >= 3/10) {
$ExposureTime = round(($arrExposureTime[0]/$arrExposureTime[1]), 1) . ' sec';
// If all else fails, just display it as it was found.
} else {
$ExposureTime = '<sup>' . $arrExposureTime[0] . '</sup>/' . $arrExposureTime[1] . ' sec';
}
This is the code I use to normalize the exposure,
if (($bottom % $top) == 0) {
$data = '1/'.round($bottom/$top, 0).' sec';
} else {
if ($bottom == 1) {
$data = $top.' sec';
} else {
$data = $top.'/'.$bottom.' sec';
}
}
It handles most exposures correctly but I see some weird ones once a while.
You can use Euclid's algorithm to find the greatest common divisor, which will help you reduce the fraction.