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
Related
I know this isn't EXACTLY a programming question, but I need help in my (php) code and I will explain it in the simplest way I can (Without code).
I have a variable that goes from 0 to 720 and I want, that the number begins from 0 after 360.
Like this:
0 = 0
1 = 1
2 = 2
...
359 = 359
360 = 0 (or 360)
361 = 1
...
719 = 359
720 = 0 (or 360)
Im scratching my head about this. I know, that's easy, I even made it many times before, but i just can't remember it. I also googled about it and searched on StackOverflow. I don't find it, because I don't know exactly what I should type to come to my solution. "Restarting" a number doesn't exist, but I can't imagine a better tag for my question - for now.
Thanks in advance.
Use the % Modulus operator....
$x = 999;
$x %= 360;
echo $x;
You can create a function to return a number instead of changing the variable:
function check360($number) {
if($number < 360) return $number;
return 360 % $number;
}
You can add another check to return false if $number >= 720 if you want.
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
)
I have a value that i need to translate to a percentage given a certain set of rules.
VALUE=X
Where X can be anything starting from 0
If:
X > 200
the result of my function should be 100 (percent).
If:
X < 200 AND >= 100
the result should be between 100 and 50 (percent).
Example: X=150 would be 75%
If:
X < 100 AND >= 80
the result should be between 50 and 25 (percent).
Example: X=90 would be 37.5%
And if:
X < 80
the result should be between 25 and 0 (percent).
My approach in PHP would be something like
if($value > 200) return 100;
if($value > 100 && $value < 200) return ???;
... and so on.
Wherein ??? obviously stands for the formula i don't know how to set up.
Is there a way to do this in one single formula?
And even if not - what is the mathematical approach to this problem?
I know it is very basic but it seems i skipped too many maths lessons in primary school.
The function can be defined piecewisely:
Graphically, this is represented with four straight lines of different gradients. While there isn't a way to represent this function without separate pieces, implementing the above is quite easy.
In PHP, this function can be written as:
function f($x) {
if ($x >= 200)
return 100;
if ($x >= 100 && $x < 200)
return 50 + 50 * (($x - 100) / 100);
if ($x >= 80 && $x < 100)
return 25 + 25 * (($x - 80) / 20);
if ($x < 80)
return 25 * ($x / 80);
}
First, determine which range you should be in. Then:
Subtract the bottom of the range.
Divide by the length of the range.
Multiply by the number of points difference over the range.
Add the number of points at the bottom of the range.
So X from 100 to 200 gives a score from 100 to 50. The bottom of the range is 100; the size of the range is 100 (200 - 100), and the score goes from 100 at the beginning to 50 at the end, so the difference is -50. Therefore -50 * ($value - 100) / 100 + 100 would work, or simplifying, -0.5 * ($value - 100) + 100 or 150 - $value * 0.5.
Working another example, from 80 to 100: the bottom of the range is 80, the size of the range is 20, and the score goes from 25 to 50 (difference: -25). You get -25 * ($value - 80) / 20 + 25, which simplifies to -1.25 * ($value - 80) + 25 or 125 - $value * 1.25
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.
I know question title seems quite 'un-understandable', but I don't know how to write question title for this particular question.
Question:
I want to find factor for position.
Let me clear you with an example.
Value Factor
[Available] [Have to find out]
----------------------------------
1 10
3 10
9 10
10 10
11 10
25 10
50 10
75 10
99 10
100 100
101 100
105 100
111 100
127 100
389 100
692 100
905 100
999 100
1000 1000
1099 1000
1111 1000
4500 1000
6825 1000
7789 1000
9999 1000
10000 10000
10099 10000
51234 10000
98524 10000
99999 10000
100000 100000
and so on.
I hope you understand what I mean to get.
Assuming that the first three values should be 1 (as noted by Asaph), then you just need to use all that logarithm stuff you learned in school:
pow(10, floor(log10($n)))
So, how does this work? The base-10 logarithm of a number x is the y such that 10^y = x (where ^ stands for exponentiation). This gives us the following:
log( 1) 0
log( 10) 1
log(100) 2
...
So the log10 of a number between 1 and 10 will be between 0 and 1, the log10 of a number between 10 and 100 will be between 1 and 2, etc. The floor function will give you the integer part of the logarithm (we're only dealing with non-negative values here so there's no need to worry about which direction floor goes with negative values) so floor(log10()) will be 0 for for anything between 1 and 10, 1 for anything between 10 and 100, etc. Now we have how many factors of ten we need so a simple pow(10, ...) gives us the final result.
References:
log10
floor
pow
I'm still a little unsure of what you're asking, but it seems like you want to map values to other values... In php arrays can be indexed with anything (making them a map). If 999 always means a factor of 100 and 1099 always means a factor of 1000, you can set the value of array[999] to 100 and the value of array[1099] to 1000, etc.
Basically Factor is 10 to the power of number of digits in $value minus 1 (except for the single digit numbers):
if($value < 10) {
$value += 10;
}
$numOfDigits = count(str_split($value,1));
$factor = pow(10,$numDigits-1);
This function should work for you. It seems like the first 3 "factors" in your list don't fit the pattern. If, in your sample data set, those first 3 "factors" should really be 1 instead of 10, then you can safely remove the first 3 lines of the body of the function below.
function getFactor($num) {
if ($num < 10) { // If the first 3 "factors" listed
return 10; // in the question should be 1 instead of 10
} // then remove these 3 lines.
$factor = 1;
while($factor <= $num) {
$factor *= 10;
}
return $factor / 10;
}