Round number in php depending on the value - php

I know the following code will remove the numbers are the point.
round($number);
I want to round the numbers as follows
if number is 20.123
I want result 20,
If number is 20.567
I want result 21
Means if value is below .5 , it should remove that value.
If value if .5 or above it should round up.
How ?
Anyone help ?

round($number) will do what you want:
round(20.156); // 20
round(20.651); // 21
Live example

I am sure it's working well.
<?php
$var = 22.443;
$var = number_format($var, 0, '.', '');
echo $var;
?>

function get_decimal_num($number){
$num = explode('.', $number);
return $num[1];
}
$num = 10.5 ; // for example
$val = get_decimal_num($num);
if($val >= 5)
{
$value = (int) $num;
echo $value = $value + 1;
}
if($val < 5)
{
echo $num;
}
Plese check it...

Try this:
use the ceil, floor, explode and substr function to achieve you value.
$num = "20.123";
$arr = explode(".", $num);
if(substr($arr[1], 0, 1) >= 5){
$num = ceil($num);
}else{
$num = floor($num);
}
echo $num;
Result:
20
Also you can use the round function.
round(20.156); // 20
round(20.651); // 21

Related

How to round numbers?

7000 / 253000 = 0.027667984189723
$number1 = "7000";
$number2 = "253000";
$total = $number1 / $number2;
// return 0.027667984189723
This result how to round 2766 ?
Try this
echo round($total,5);
$number1 / $number2 * 10000 % 1
I'm not sure how you came up with 2766 as a result of your decimal. But I'm up for that challenge. Firstly, to get the correct decimal placing do:
<?php echo round($total, 5); ?>
However, if you're like me, I'm going to figure out that challenge and have some fun with it:
$num1 = round($total, 5);
$num2 = strval($num1);
$num3 = str_split($num2);
$arr1 = array();
foreach ($num3 as $num) {
if(is_string($num)) {
array_push($arr1, $num);
}
}
$arr2 = array_slice($arr1, 3);
foreach($arr2 as $out) {
echo $out;
}
The expected output is 2767, since I did it with the correct round function.
I hope it works for you i tried with different values its working good when value start with 0.
$after_point = ltrim($total,"0."); //27667984189723
echo substr($v,0,4); //2766
if you give 52.548457 then it return 52.5
You must chack that number start with 0.

Check value within range or not php

I want to check value with in range or not suppose if I have range D1 to D40 and if I enter D20 then it returns value with in range.
I check several solution but this are for only integer not for both string and integer.
EDIT
Range will be dynamic like AA20 to AA30 or like AC10D to AC30D
You can write something simpler like this...
$arr = range(1,40); //<--- Creating a range of 1 to 40 elements..
array_walk($arr,function (&$v){ $v = 'D'.$v;}); //<--- Concatenating D to all the elements..
echo in_array('D20',$arr) ? 'Found' : 'Not Found'; //<-- The search part.
Demonstration
First, you should remove the letter D from your string variable, like this:
// This is your first variable:
$rang1="D5";
// This is your second rang variable:
$rang2="D20";
$rang1=str_replace("D","",$rang1);
$rang2=str_replace("D","",$rang2);
$rang=$rang2-$rang1;
echo $rang;
Or if your variable looks like this:
$rang="D5 TO D20";
you can use the following:
$rang="D5 TO D20";
$rang=explode(" TO ",$rang);
$rang1=rang[0];
$rang2=rang[1];
$rang1=str_replace("D","",$rang1);
$rang2=str_replace("D","",$rang2);
$rang=$rang2-$rang1;
echo $rang;
// 1. build up array of valid entries
$prefix = "D";
$rangeArray = array();
for($i = 1; $i <= 40; $i++) {
$rangeArray[] = $prefix . $i;
}
...
// 2. check against that array:
$inRange = in_array($needle, $rangeArray); // boolean
To get the position in the range:
$pos = array_search($needle, $rangeArray); // integer or false if not found
Where $needle would be your input value.
The following code will work with ranges with different letter in the beginning like A10 to B30 (assuming A20 is in that range, but A40 is not):
$min = "A10";
$max = "B30";
$test = "A20";
$min_ascii = chr($min[0]);
$max_ascii = chr($max[0]);
$test_ascii = chr($max[0]);
$min_number = substr($min, 1);
$max_number = substr($max, 1);
$test_number = substr($test, 1);
if ($min_ascii <= $test_ascii and $test_ascii <= $max_ascii
and $min_number <= $test_number and $test_number <= $max_number)
{
echo "$test is in the range from $min to $max";
}

how to remove more 1 decimal numbers on a number

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

PHP math (numbering)

$temp is currently 6. But the variable result can be changing every time to a different number so it is not a fixed value.
Anyway, for this $temp * 1.1666666, the result will be 6.99999996. Since I used the floor function, it will be rounded down to 6.
Is there any way when the value is more then>*.49999 it will stay at *.5 instead of *?
Example: 6.51111111, 6.78948123, 6.9747124
Expected Output: 6.5
Example: 6.49999999, 6.12412431, 6.33452361
Expected Output: 6
Do note that, $temp value will be ever changing..thank you!
Use round($number, 1). That will round to the nearest decimal point.
$number = round(.1666666 * $temp, 1);
If you want to round to the nearest half you can do this:
function round_to_half($num)
{
if($num >= ($half = ($ceil = ceil($num))- 0.5) + 0.25) return $ceil;
else if($num < $half - 0.25) return floor($num);
else return $half;
}
$number = round_to_half(.1666666 * $temp);
Try this code...
<?php
$temp = 6.94444;
echo myRound($temp);
function myRound($temp)
{
$frac = $temp - floor($temp);
$frac = ($frac >= .5) ? .5 : 0;
return ( floor($temp) + $frac );
}
?>
Hope this is what you want.

Get the sum of all digits in a numeric string

How do I find the sum of all the digits in a number in PHP?
array_sum(str_split($number));
This assumes the number is positive (or, more accurately, that the conversion of $number into a string generates only digits).
Artefactos method is obviously unbeatable, but here an version how one could do it "manually":
$number = 1234567890;
$sum = 0;
do {
$sum += $number % 10;
}
while ($number = (int) ($number / 10));
This is actually faster than Artefactos method (at least for 1234567890), because it saves two function calls.
Another way, not so fast, not single line simple
<?php
$n = 123;
$nstr = $n . "";
$sum = 0;
for ($i = 0; $i < strlen($nstr); ++$i)
{
$sum += $nstr[$i];
}
echo $sum;
?>
It also assumes the number is positive.
function addDigits($num) {
if ($num % 9 == 0 && $num > 0) {
return 9;
} else {
return $num % 9;
}
}
only O(n)
at LeetCode submit result:
Runtime: 4 ms, faster than 92.86% of PHP online submissions for Add Digits.
Memory Usage: 14.3 MB, less than 100.00% of PHP online submissions for Add Digits.
<?php
// PHP program to calculate the sum of digits
function sum($num) {
$sum = 0;
for ($i = 0; $i < strlen($num); $i++){
$sum += $num[$i];
}
return $sum;
}
// Driver Code
$num = "925";
echo sum($num);
?>
Result will be 9+2+5 = 16
Try the following code:
<?php
$num = 525;
$sum = 0;
while ($num > 0)
{
$sum= $sum + ($num % 10);
$num= $num / 10;
}
echo "Summation=" . $sum;
?>
If interested with regex:
array_sum(preg_split("//", $number));
<?php
echo"----Sum of digit using php----";
echo"<br/ >";
$num=98765;
$sum=0;
$rem=0;
for($i=0;$i<=$num;$i++)
{
$rem=$num%10;
$sum=$sum+$rem;
$num=$num/10;
}
echo "The sum of digit 98765 is ".$sum;
?>
-----------------Output-------------
----Sum of digit using php----
The sum of digit 98765 is 35
// math before code
// base of digit sums is 9
// the product of all numbers multiplied by 9 equals 9 as digit sum
$nr = 58821.5712; // any number
// Initiallization
$d = array();
$d = explode(".",$nr); // cut decimal digits
$fl = strlen($d[1]); // count decimal digits
$pow = pow(10 ,$fl); // power up for integer
$nr = $nr * $pow; // make float become integer
// The Code
$ds = $nr % 9; // modulo of 9
if($ds == 0) $ds=9; // cancel out zeros
echo $ds;
Assume you want to find the sum of the digits of a number say 2395 the simplest solution would be to first split the digits and find out the sum then concatenate all the numbers into one single number.
<?php
$number=2;
$number1=3;
$number2=9;
$number3=5;
$combine=$number.$number1.$number2.$number3;
$sum=$number+$number1+$number2+$number3;
echo "The sum of $combine is $sum";
?>
One way of getting sum of digit however this is a slowest route.
$n=123;
while(($n=$n-9)>9);
echo "n: $n";
<html>
<head>
<title>detail</title>
</head>
<body>
<?php
$n = 123;
$sum=0; $n1=0;
for ($i =0; $i<=strlen($n);$i++)
{
$n1=$n%10;
$sum += $n1;
$n=$n/10;
}
echo $sum;
?>
</body>
</html>
Here's the code.. Please try this
<?php
$d=0;
$num=12345;
$temp=$num;
$sum=0;
while($temp>1)
{
$temp=$temp/10;
$d++;
}
echo "Digits Are : $d </br>";
for (;$num>1;)
{
$d=$num%10;
$num=$num/10;
$sum=$sum+$d;
}
echo "Sum of Digits is : $sum";
?>

Categories