If a number is too large use an abbreviation - php

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

Related

PHP slightly increment value based on number of decimals

I am trying to slightly increment a value based on the number of decimals it has.
For example if the value is 1.2 I would increase it by 0.1, 12.345 by 0.001, 12.345678 by 0.000001, etc.
I currently have a long implementation using a chain of if, else if. I know this is not the most efficient way and a loop can be used, but I was unsure of how to structure the loop. I tried using the PHP substr_replace function, but I could not get it to work for this.
Is there another way I can structure a loop to reduce my lines of code and be more efficient?
Here is my php code so far:
$valueOne = 12.345678;
// get amount of decimals
$decimal = strlen(strrchr($valueOne, '.')) -1;
/*
this also works for finding how many decimals
$test = floatval($valueOne);
for ( $decimal_count = 0; $test != round($test, $decimal_count); $decimal_count++ );
echo $decimal_count;
*/
// see value before change
echo $valueOne;
if ($decimal == "1") {
$valueOne = $valueOne + 0.1;
}
else if ($decimal == "2") {
$valueOne = $valueOne + 0.01;
}
else if ($decimal == "3") {
$valueOne = $valueOne + 0.001;
}
// etc ...
// see value after change
echo $valueOne;
/*
i tried messing around with using a loop, but did not have much luck
$start = 0.1;
$count = 0;
$position = 2;
while ($count != $decimal) {
echo substr_replace($start, 0, $position, 0) . "<br />\n";
$count++;
//$position++;
}
*/
Get the number of digits after the decimal. Then create a number with a decimal point, one less 0, followed by 1, to get the amount to add.
$valueOne = 12.345678;
// get amount of decimals
$decimal = strlen(strrchr($valueOne, '.')) -1;
// see value before change
echo $valueOne . "<br>\n";
// Get amount to add
$increment = '.' . str_repeat('0', $decimal-1) . '1';
$valueOne += $increment;
echo $valueOne;
Get the number of decimals
Multiply by the appropriate factor so the number is now an integer
Increment by 1
Divide by the same factor to get back to the original number (properly incremented)
function increment($number){
// get amount of decimals
$decimal = strlen(strrchr($valueOne, '.')) -1;
$factor = pow(10,$decimal);
$incremented = (($factor * $number) + 1) / $factor;
return $incremented;
}

Rounding function

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;
}

Formating Comment Count in WordPress with PHP

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;
}

PHP Money Format

Basically, I need a function to help me achieve this:
I have a raw integer, say 5000, which is an amount of money. It's not formatted in any way. I want to be able to convert 5000 into a string like "$5k".
If the amount was 1000000 (one million), the string would be "$1m".
If the amount was 1000000000 (one billion), the string would be "$1b";
I also need to round the number, so say the amount was 5555555, the string would be "$5.5m"
A simple way to do that id to create a format function like this :
function format($amount) {
if ($amount < 1000) {
return '$' . $amount;
} else if($amount < 1000000) {
$amount /= 1000;
return '$' . round($amount, 1) . 'k';
} else if($amount < 1000000000) {
$amount /= 1000000;
return '$' . round($amount, 1) . 'm';
} else {
$amount /= 1000000000;
return '$' . round($amount, 1) . 'b';
}
}
If you're running PHP on a system with strfmon capabilities, you can use the money_format method (windows don't support it).

Parsing EXIF's "ExposureTime" using PHP

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.

Categories