Total a natural number with real number by php? - php

How can increase a number according to percent with PHP?
For example: 100 + 20% = 120 or 1367 + 33% = 1818.11
If output was this: 1818.11 i want only this: 1818 or this 12.32 = 12 or 546.98 = 546
How is it?

I'm not sure if you're looking for something deeper, but the answer seems fairly straightforward...
$increasedValue = floor( $value * ( 1 + ( $percentageIncrease / 100 ) ) );
If you wrapped that in a function, say increaseByPercentage( $value, $percentageIncrease ), you would get...
increaseByPercentage( 100, 20 ); //returns 120
increaseByPercentage( 1367, 33 ); //returns 1818
Edit
Based on your comment, I'm not sure what else you are looking for. PHP has 6 arithmetic operators:
Negation (-$a)
-5 = -5
Addition ($a + $b)
5 + 3 = 8
Subtraction ($a - $b)
5 - 3 = 2
Multiplication ($a * $b)
5 * 3 = 15
Division ($a / $b)
5 / 3 = 1.667
Modulus ($a % $b)
5 % 3 = 2
If you write a line of code that says $a = 100 + 20%; it will not parse.

function addPercent($number, $percent)
{
return (int)($number+$number*($percent/100));
}
echo addPercent(1367, 33); // 1818
Just cast the result into an Integer.

Edit: My method isn't quite right. It looks like Farray has outlined a better one as well.
Is this what you want?
round( 156 + 156 * ( 20/100 ) )
Normally without round() this would = 187.2. With round(), it's equal to 187.

Related

Understanding something more about the % Modulus operator

I am learning to work with some math like PHP query and just got to the modulo, I am not quite sure in what situations to use this because of something i stumbled on and yes I did already read one of the posts here about the modulo :
Understanding The Modulus Operator %
(This explanation is only for positive numbers since it depends on the language otherwise)
The quote above is in the top answer there. But if I focus on PHP only and i use the modulo like this:
$x = 8;
$y = 10;
$z = $x % $y;
echo $z; // this outputs 8 and I semi know why.
Calculation: (8/10) 0 //times does 10 fit in 8.
0 * 10 = 0 //So this is the number that has to be taken off of the 8
8 - 0 = 8 //<-- answer
Calculation 2: (3.2/2.4) 1 //times does this fit
1 * 2.4 = 2.4 //So this is the number that has to be taken off of the 3.2
3.2 - 2.4 = 0.8 // but returns 1?
So my question is why does this exactly happen. my guess would be that in the first phase it would get 8/10 = 0,8 but this doesn't happen. So can someone explain a bit about why this happens. I understand the modulo's basics like if I do 10 % 8 = 2 and I semi understand why it doesn't return something like this: 8 % 10 = -2.
Also, is there a way to modify how the modulo works? so it would return a - value or a decimal value in the calculation? or would I need to use something else for this
Little shortened: why does this happen when I get a negative number in return and is there some other way or operator that can actually do the same and get in the negative numbers.
Modulus (%) only works for integers, so your calculation at the bottom of your example is correct...
8/10 = 0 ( integer only ), remainder = 8-(0*10) = 8.
If you instead had -ve 12 - -12%10...
-12/10 = -1 (again integer only), remainder = -12 - (10*-1) = -2
For floats - you can use fmod(http://php.net/manual/en/function.fmod.php)
<?php
$x = 5.7;
$y = 1.3;
$r = fmod($x, $y);
// $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7
(Example from manual)

is there a php function to return the difference between any 2 integer numbers as a positive integer?

I have googled, yahooed and researched SO but no luck. I am trying to compare 2 numbers using PHP.
To be clear I know I can accomplish this using basic maths and maybe a simple
if{}
I know how to do this, I could write a simple function, finding the result but this is not my question.
My question is simply - Is there a PHP function to return the difference between 2 integer numbers, +ve or -ve presented in any order as a positive integer
Example
PHPFunction(3,-2) result 5
Thanks
As pointed out by #Phylogenesis, you can use the abs() function. For example:
$var1 = -2;
$var2 = -30;
echo abs($var1 - $var2); // 28
You could also define your own function:
function abs_diff($v1, $v2) {
$diff = $v1 - $v2;
return $diff < 0 ? (-1) * $diff : $diff;
}
echo abs_diff(-2, -30); // 28
Use the absolute value function of php of the difference of the two numbers.
$answer = abs($num1 - $num2);
Subtracting one number from another may not be the answer.
Measuring the distance between 1 and -1 (not subtraction) is 2?
-3 -2 -1 0 1 2 3 How far apart are 1 and -1? The distance is 1 if zero is not a number or 2 if it is.
$var1 = -2;
$var2 = -30;
echo abs($var1 - $var2); // 28
Yes the result is 28 but the distance between $var1 and $var2 is 2. Math function for distance?

PHP auto convert price to 99 rather than 100

I have different prices:
100
550
799
1200
350
Wonder how i can automatically convert them to
99
549
799
1200
349
I'd just do -1, but some are already correct like 799, and i don't want it to be 798.
Cant think of way to do this conversion i am good with PHP but not great with math. I bet we need to find if it divides by 2 if yes then subtract 1 if not then do nothing.
if ($price % 2) {
$price = intval($price) - 1;
}
Is this the best way to do it?
You could do something like this :
if (($price % 50) == 0)
{
$price--;
}
Everytime the modulo of 50 will be equal to zero (this will be the case for any number that ends with "50" or "100"), it will remove one.
Change the second value (50 in this case) if you want to target more or less prices.
Something like this?
if(($price % 10) == 0) {
$price--;
}
As you don't want to have 798, your code does not work
But you can use this.
if ($price % 10) {
$price = intval($price) - 1;
}
799 % 2 = 1
800 % 2 = 0
798 % 2 = 0
799 % 10 = 0
800 % 10 = 1
798 % 10 = 0
or %50 if you only want to subtract 1 from numbers like 750 and 800

reference value in php for scoring (min x = 1, max y = 100)

I'd like to do a tiny scoring, but I stumble at maths. I guess you will handle it :-) I have a few values: e.g. 124, 426, 100, 24, 41. The highest shell be MaxScore = 100 (here 426), and the minimum MinScore = 1 (here 24). Every value (here 124,100,41) between MaxScore and MinScore shell get an own Score, related to min and max.
With php max(), I get
$max = max(124, 426, 100, 24, 41);
426 as the highest and with min()
$min = min(124, 426, 100, 24, 41);
24 as the lowest value. Fine.
Next step could be like: 426 = 100 and 24 = 1. But the rest? What's the calculation? As you can see, it's not a programmer problem (not yet), rather a maths-issue. I hope I did make myself clear. Thank you!
Ok this seems to work. Might have put more parenthesis in than needed.
$new_val = 1 + (($val - $min) * (99 / ($max - $min)))
One thing you could do is apply each value to be $val * ($max - $min) / 100;
EDIT I had that backwards. It'll be $val / (($max - $min) / 100); This takes the ratio of the difference between $min and $max divided into 100 pieces. So each piece will then have the ratio applied to it. This would make 100 be 100 / ((426 - 24) / 100) = 24.875 and it would reduce all values to a number between 1 and 100.
EDIT. To get the min to be 1 and the max to be 100 and maintain relationships between scores we need to apply the ratio and subtract a contstant.
Verified Andrew Nee's comment seems to do the trick.
php -r "$a = array(123,426,100,24,41);print_r(array_map(function($val){$min=24;$max=426;return 1 + (($val - $min) * (99/($max - $min)));},$a));"
Array
(
[0] => 25.380597014925
[1] => 100
[2] => 19.716417910448
[3] => 1
[4] => 5.1865671641791
)

What does the percent sign mean in PHP?

What exactly does this mean?
$number = ( 3 - 2 + 7 ) % 7;
It's the modulus operator, as mentioned, which returns the remainder of a division operation.
Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3.
5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.
10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder.
In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7, so $number will be 1, which is the remainder of 8/7.
It is the modulus operator:
$a % $b = Remainder of $a
divided by $b.
It is often used to get "one element every N elements". For instance, to only get one element each three elements:
for ($i=0 ; $i<10 ; $i++) {
if ($i % 3 === 0) {
echo $i . '<br />';
}
}
Which gets this output:
0
3
6
9
(Yeah, OK, $i+=3 would have done the trick; but this was just a demo.)
It is the modulus operator. In the statement $a % $b the result is the remainder when $a is divided by $b
Using this operator one can easily calculate odd or even days in month for example, if needed for schedule or something:
<?php echo (date(j) % 2 == 0) ? 'Today is even date' : 'Today is odd date'; ?>
% means modulus.
Modulus is the fancy name for "remainder after divide" in mathematics.
(numerator) mod (denominator) = (remainder)
In PHP
<?php
$n = 13;
$d = 7
$r = "$n % $d";
echo "$r is ($n mod $d).";
?>
In this case, this script will echo
6 is (13 mod 7).
Where $r is for the remainder (answer), $n for the numerator and $d for the denominator. The modulus operator is commonly used in public-key cryptography due to its special characteristic as a one-way function.
Since so many people say "modulus finds the remainder of the divisor", let's start by defining exactly what a remainder is.
In mathematics, the remainder is the amount "left over" after
performing some computation. In arithmetic, the remainder is the
integer "left over" after dividing one integer by another to produce
an integer quotient (integer division).
See: http://en.wikipedia.org/wiki/Remainder
So % (integer modulus) is a simple way of asking, "How much of the divisor is left over after dividing?"
To use the OP's computation of (3 - 2 + 7) = 8 % 7 = 1:
It can be broken down into:
(3 - 2 + 7) = 8
8 / 7 = 1.143 #Rounded up
.143 * 7 = 1.001 #Which results in an integer of 1
7 can go into 8 1 time with .14 of 7 leftover
That's all there is to it. I hope this helps to simplify how exactly modulus works.
Additional examples using different divisors with 21.
Breakdown of 21 % 3 = 0:
21 / 3 = 7.0
3 * 0 = 0
(3 can go into 21 7 times with 0 of 3 leftover)
Breakdown of 21 % 6 = 3:
21 / 6 = 3.5
.5 * 6 = 3
(6 can go into 21 3 times with .5 of 6 leftover)
Breakdown of 21 % 8 = 5:
21 / 8 = 2.625
.625 * 8 = 5
(8 can go into 21 2 times with .625 of 8 leftover)

Categories