I can find reminder by "%" operator . But how can I find the quotient at the same time . Suppose I will divide 10 by 3 . If there is any function which will give me the output 3 as quotient and 1 as reminder .
$remainder = $a % $b;
$quotient = ($a - $remainder) / $b;
Use type casting:
$quotient = (int)(10/3)
This will divide 10 by 3 and then cast that result to an integer.
Since functions can only return a single value (not counting pass-by-reference functionality), there's no way to return 2 separate values from a single function call (getting both the quotient and remainder at the same time). If you need to calculate two different values, then you will need at least two statements.
You can, however, return an array of values and use PHP's list function to retrieve the results in what looks like a single statement:
function getQuotientAndRemainder($divisor, $dividend) {
$quotient = (int)($divisor / $dividend);
$remainder = $divisor % $dividend;
return array( $quotient, $remainder );
}
list($quotient, $remainder) = getQuotientAndRemainder(10, 3);
How about gmp_div_qr function
<?php
$res = gmp_div_qr(11,5);
die(var_dump($res));
the accepted answer will not work with float values.
another way with inbuilt php>(v.4.2.0) method
fmod
$remainder = fmod ( float $x , float $y );
$quotient = ($x - $remainder) / $y;
Try this
$x=10;
$y=3;
$Quotient =(int)($x/$y);
$Remainder = $x % $y;
Related
I am new to PHP and stackoverflow and try to figure things out for myself before asking but I am having a little trouble doing some maths on an array I have pulled from a database with PHP.
So far I have an array of numbers called $array['sn']
I have created a function in excel that does the maths and works well in excel but I cant figure out a way to do it in PHP.
the excel function is =QUOTIENT(E32,65536)"IENT(E32-F34*65536,256)&(G33-G35*256)
E32 being the value I start with i.e $sn
F34 being the answer to the first quotient
G35 being the answer to the second quotient
G33 being E32-F34*65536
I want to take a number e.g. 3675177 divide it by 65536 but without the remainder which is 56, then multiply 56 by 65536 which equals 3670016, then find the difference between 3670016 and 3675177 which is 5161. Then divide 5161 by 256 with no remainder which is 20 then multiply 20 by 256 and subtract 5161 which is 41.
The end result from 3675177 should be 562041. I want to do this calculation on every number in the $array['sn'], any help would be appreciated.
The calculation and formatting of the output would be like this:
$n = 3675177;
$const = 65536;
$const2 = 256;
$a = intval($n / $const); // intval returns only the integer part of a number
$x = $n % $const; // $n % $const means "the remainder of $n / $const"
$b = intval($x / $const2);
$c = $x % $const2;
// Two options to handle values of $c < 10:
// if ($c < 10) $c = "0$c";
// $c = str_pad($c, 2, "0", STR_PAD_LEFT);
echo "$a$b$c";
I would recommend using array_map to apply the calculation to your array of values.
There are php arithmetic operations you can use.
I would do something like this:
$initialNumber = //the initial number, wherever you get it from
$entireDivision = ceil($initialNumber/65536)-1;
$remainder = $initialNumber%65536;
$remainderMultiplied = $remainder * 56;
$difference = $initialNumber - $remainderMultiplied;
$differenceDivided = ceil($difference/256)-1;
$differenceMultipliedAndSubstracted = ($differenceDivided * 256) - $difference;
Maybe I used too many variables, this is to be a bit more easy to understand for you. Maybe I did some operation wrong, check it out too. But this is the idea of mathematic operations in php. Maybe you should put this inside a php function with parameters, so your code gets cleaner if you use multiple times.
EDIT: You should put this code inside a function, then run a foreach loop in your array running this function taking as parameter the value of the array position.
$results = array();
foreach ($array['sn'] as $key => $a) {
$b = intval($a / 65536);
$c = ($a - $b * 65536);
$d = intval($c / 256);
$e = $c - $d * 256;
$results[$key] = $b . $d . $e;
}
var_dump($results);
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));
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
without use of round() function perfrom the round() in php
$a = "123.45785";
$v = round($a);
output: 123.46;
it had done by round function but i want to get output without use of round and number_format() function.
Here's a way of doing it with arithmetics:
function my_round($num, $places = 2) {
// Multiply to "move" decimals to the integer part
// (Save one extra digit for rounding)
$num *= pow(10, $places + 1);
// Truncate to remove decimal part
$num = (int) $num;
// Do rounding based on the last digit
$lastDigit = $num % 10;
if ($lastDigit >= 5)
$num += 10;
// Remove last digit
$num = (int) ($num/10);
// "Move" decimals in place, and you're done
$num /= pow(10, $places);
return $num;
}
You have sprintf.
$a = "123.45785";
echo sprintf("%01.2f", $a); // output: 123.46
I am generating some data of latitude and longitude with rand(10000000, 3000000); for example. But I need to calculate distance between two locations, so basically I need to convert my result, for example 22049256 to 22.049256 in order to pass to my function.
How can I achieve this most effectively with using least amount of resources?
TL;DR
I have integer 22049256, needs to be converted to float 22.049256.
Divide by 1M?
22049256 / 1000000 = 22.049256
$foo = rand(10000000, 30000000);
$foo /= 1000000;
echo $foo;
If you always want the decimal after the first two digits, that should do the trick.
<?php
$n = rand(10000000, 3000000);
$len = strlen($n);
$div = pow(10, $len - 2);
$n /= $div;
var_dump($n);
?>
http://codepad.viper-7.com/Nrql15
Basically just dividing by a number made by checking how many characters there are in your original number.
Simplest way would be to treat the number as a string and iterate through it, adding the decimal point at the appropriate position.
$new_number = '';
for($i=0;$i<strlen((string)$number;$i++)
{
if($i == 2)
$new_number .= '.';
$new_number .= substr((string)$number, $i, 1);
}
Edit:
Another possibility is to divide by 1000000 and run number_format to ensure proper decimal places:
$new_number = number_format($number/1000000, 6);
I know ceil, which will round 15.1 to 16 and 31.2 to 32.
But how to round up to the next x20 number? like 15.1 to 20 and 31,2 to 40?
Is needed to make sensefull labels for the y-axis of a chart.
Try this (works in JavaScript):
$result = 20 * ceil($input / 20);
Here, we're rounding to 20. To round to other numbers, simply replace 20 with whatever base you want. The documentation for ceil() can be found here.
A function that does the same:
function roundTo($value, $base)
{
return $base * ceil($value / $base);
}
As a small aside, if you want to round to the nearest base, instead of rounding up, use round() instead of ceil().
divide by 20 and use ceil, then multiply by 20
Or you could use modulus:
echo round($a + 20 - ($a % 20));
If you want "mid-range" values to round more, you can use the following.
function roundTo($n,$i = 1){
$r = $n % $i;
$d = round(($n - $r * $i) / $i);
return $r * $i + $d * $i;
}
for ($a = 0; $a < 100; $a++){
printf("%d = %d\r\n", $a, ceil2($a, 20));
}
The above produces:
Number: Rounded To:
0-10 0
11-30 20
31-50 40
etc.
This more closely simulates how 1.3=1 but 1.5=2.
you can use this
ceil(x/2)*2