How to get the number before dot symbol, i try using round() but sometimes give me wrong number for what i need, i have the following calculation, lets say
$Calc = 10/3;
$Calc = 3.333333333333333;
I'm trying to use round() php function
$Calc = 10/3;
$Calc = round(3.333333333333333);
$Calc = 3;
but for example if my result is 3.51+
$Calc = round(3.51);
$Calc = 4;
BUT i just need the 3, not the rest , how can i get that?
you could try with intval() function
echo intval(3.51); // output will be 3
echo intval(10/3); // output will be 3
Use this code:
$Calc = 10/3;
$pos = strpos($Calc, '.');
$Calc = substr($Calc, 0, $pos);
This will give the all the digits before the decimal.
Use floor:
echo floor(3.99); // 3
echo floor(3.023);// 3
echo floor(4.4); // 4
echo floor(9.0); // 9
Related
#if($user->dettagli->facebook_follower >= 1000 && $user->dettagli->facebook_follower <= 999999)
<?php
echo(round($user->dettagli->facebook_follower, 5,PHP_ROUND_HALF_DOWN) . "K");
?>
{{$user->dettagli->facebook_follower / 1000}} K
#endif
Hi could someone please help me. I am using Laravel and trying to display a users Facebook follower count, but I got a little stuck with something. I am taking their int at for example 1589 followers and trying to return in the blade "1.5k". On screen I return either 1589k with this php call or 1.589k. How do I get that number to one decimal place and become only 1.5k??
thank you!
As per the docs
Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).
You are specifying a precision of 5. You should change this to 1.
Change your function call to:
echo(round($user->dettagli->facebook_follower, 1, PHP_ROUND_HALF_DOWN) . "K");
EDIT: I mis-read the question. This is a function that should handle numbers into the millions.
You would need to use laravel-twigbridge to make the function available in your templates though.
function pretty_number(int $n): string {
$prettyN = $n;
$suffix = '';
$len = strlen((string) $n);
$suffixes = ['K', 'M'];
foreach ($suffixes as $s) {
if ($n < 1000) {
break;
}
$suffix = $s;
$n = $n / 1000;
$prettyN = number_format($n, 1);
}
return $prettyN . $suffix;
}
echo pretty_number(100); // 100
echo pretty_number(999); // 99
echo pretty_number(1589); // 1.6K
echo pretty_number(1600); // 1.6K
echo pretty_number(1589300); // 1.6M
You can use the NumberFormatter. This can be defined in a global place so you only need to use the third line in your blade view.
$a = new \NumberFormatter("en-US", \NumberFormatter::DECIMAL);
$a->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 1);
echo $a->format($user->dettagli->facebook_follower);
I want to round up my variable if it's decimal larger than .3 and if it's lower or equal it will round down, for example if i have 1.34 it will round up to 2, if i have 1.29 it will round down to 1, and if i have 1.3 it will round down to 1. I don't know how to do this precisely, right now i'm using the round basic function like this:
$weight = $weight/1000;
if($weight < 1) $weight = 1;
else $weight = round($weight, 0, PHP_ROUND_HALF_DOWN);
If you manipulate the numbers a bit, you can figure out if the decimals are .3 or higher. You achieve this by flooring the value, and subtract that from the original value. Check if the result of that, multiplied by 10, is greater than 3. If it is, you've got something above x.3.
$number = 1.31;
$int = floor($number);
$float = $number-$int;
if ($float*10 > 3.1)
$result = ceil($number);
else
$result = $int;
echo $result; // 2
Live demo
I made you a little hack, here's the code
$weight = 5088;
$weight = $weight/1000;
if($weight < 1) {
$weight = 1;
} else {
// I get the last number (I treat the $weight as a string here)
$last_number = substr($weight, -1, 1);
// Then I get the precision (floating numbers)
$precision = strlen(substr(strrchr($weight, "."), 1));
// Then I convert it to a string so I can use some helpful string functions
$weight_str = (string) $weight;
// If the last number is less then 3
if ($last_number > 3)
// I change it to 9 I could just change it to 5 and it would work
// because round will round up if then number is 5 or greater
$weight_str[strlen($weight_str) -1] = 9;
}
}
// Then the round will round up if it's 9 or round down if it's 3 or less
$weight = round($weight_str, $precision);
echo $weight;
Maybe something like this function?
function roundImproved($value, $decimalBreakPart = 0.3) {
$whole = floor($value);
$decimal = $value - $whole;
$decimalPartLen = strlen($decimal) - 2;
return (number_format($decimal, $decimalPartLen) <= number_format($decimalBreakPart, $decimalPartLen) ? $whole : ceil($value));
}
Proof:
http://sandbox.onlinephpfunctions.com/code/d75858f175dd819de069a8a05611ac9e7053f07a
You can specify "break part" if you want.
I need to echo number(variable) in two ways and i need help with code for this equation.
Example:
Variable is 5003
First echo has to be: 5000 (rounded)
Second echo has to be just the rounded digits: 3
So i want to know if and how can i achieve this equation, im thinking among lines of: variable(5003) minus rounded variable(5000) equals 3
So that way if variable is lets say 15009
Fist will be 15000
Second will be 9
I hope this make sense, thank you for help
You should look into the roundPHP function:
You can have negative decimal points like this:
round(5003, -3); // returns 5000
round(15009, -3); // returns 15000
To figure out the difference you can do like this:
$input = 5003
$x = $input;
$y = round($input, -3);
$z = $x - $y; // z is now 3
PHP is not a mathematical language, so it cannot solve equations for you.
You can make a more general solution like this:
$inputs = [
5003,
15009,
55108,
102010
];
foreach ($inputs as $input) {
$decimals = floor(log10($input)) - 1;
$rounded = round($input, -1 * $decimals);
echo "$input - $rounded = " . ($input - $rounded) . PHP_EOL;
}
Outputs:
5003 - 5000 = 3
15009 - 15000 = 9
55108 - 55000 = 108
102010 - 100000 = 2010
Assuming that you want to round the last three digits:
$input = 5003;
$rounded = (int)(5003 / 1000) * 1000;
$rest = $input - $rounded;
echo($rounded . "\n" . $rest);
This results in:
5000
3
I would like to know how to achieve this:
round(0.38)
And receive this:
0.40
How can I do this? I tried with round(0.38, 2) but it does not work.
To get 0.4, round to 1 decimal place using round() where the second parameter is the decimal place.
$rounded_number = round(0.38, 1);
To get 0.40, use number_format() to get 2 decimal places.
$formatted_number = number_format( $rounded_number , 2);
Multiply by ten, round, then divide by ten:
php > $a = 0.38;
php > $b = $a * 10;
php > $c = round($b);
php > echo $c/10;
0.4
If you want the two decimal places:
$num = 0.38;
$num = round($num * 10)/10;
$num = number_format($num, 2);
echo $num;
It is probably far simpler than you think it is:
$num = round(0.38 * 10)/10;
echo $num;
Then, you could just replace 0.38 with another number or a variable.
$a = 0.38;
echo sprintf("%.2F", round($a,PHP_ROUND_HALF_UP));
What's the simple way to remove more than 1 decimal number from source number .
for example source numbers are :
1st source number is : 56.48216585224
2nd source number is: 93
Output must be :
1st output : 56.4
2nd output: 93
numbers are not static
what's the simple way ?
If you don't want rounding, then:
$number = 56.48216585224;
echo substr($number, 0, strpos($number, '.')+2); // output: 56.4
Otherwise:
Use php round() or number_format()
http://php.net/manual/en/function.round.php
http://php.net/manual/en/function.number-format.php
Examples:
$number = 56.48216585224;
echo number_format($number, 1, '.', ''); // Output: 56.5
echo round($number, 1); // Output: 56.5
I will suggest this PHP code for your requirement:
$n = 56.48216585224;
$m = floor($n * 10) / 10; // $m = 56.4 now
You can try the following
print(quickFormat("56.48216585224"));
print(quickFormat("93"));
function quickFormat($number) {
$number = explode(".", $number);
if (isset($number[1])) {
return $number[0] . "." . $number[1]{0};
} else {
return $number[0] ;
}
}
Output
56.4
93