I have two inputs
min, max
In the case where min=32 and max=46 then I would like PHP to automatically the value to the nearest 5/10 i.e. in this case min=30 and max=50?
But in the instance of course if min=35 or 40 and max=40 or 45 there would be no need to round off.
How do achieve this in PHP?
Btw the system only deals with integer values and the above values are just examples. It needs to work for a range of numbers ranging from 0 to infinity. So 4 would round to 0, 6 to 10... etc...
function round_to_10($n) {
return round($n / 10) * 10;
}
php> echo round_to_10(32)
30
php> echo round_to_10(46)
50
If you multiply the number by two, you can round this to 10s and then divide it by 2 again.
function round_five($num) {
return round($num*2,-1)/2;
}
$nums = array(32,46,35,40);
foreach($nums as $num) {
printf("%s: %s\n",$num,roundFive($num));
}
The above will return
32: 30
46: 45
35: 35
40: 40
Related
I have large numbers and their decimals points. Here are some examples and what should return:
Number: 300000
Decimals: 8
Should return: 0.00300000
Number: 700000000
Decimals: 8
Should return: 7
Number: 800000000
Decimals: 6
Should return: 800
At the moment I'm doing this:
substr($number, 0, abs($decimals + 1))
But it returns 0 if the number of decimals is superior to the number of digits in the number (example one returns 0).
Any idea how to do this?
Divide by 10 to the power of the number of decimals:
$result = $number / (10 ** $decimals);
Divide by ten to the power of decimals?
$n = 700000000;
$d = 8;
Echo $n/pow(10,$d);
https://3v4l.org/MdoW1
I don't know how to title the question correctly (please tell me what this kind of number called).
I'd like to convert from 2 digits number into a 10 divisible number. For example, I expect:
15 to become 10
23 to become 20
38 to become 30
999 to become 900
9999 to become 9000
I tried searching for the solution on google but I don't know how to type the proper word.
Easy, use the PHP floor function: http://php.net/manual/en/function.floor.php
floor($number/10) * 10
I wrote a simple function should work:
<?php
function roundDown($var){
$len = strlen($var)-1;
$divide = 1;
for($i=1;$i<=$len;$i++){
$divide .= 0;
}
return floor($var/$divide)*$divide;
}
echo roundDown(9999);
Easiest way is divide by ten, then floor value and multiply by ten. (Floor method - phpdocs)
floor($number/10)*10
Use round:
echo round(1241757, -6); // 1000000
from http://php.net/manual/en/function.round.php
You are looking for the "one significant figure".
I think this answer provides a good solution:
How to round down to the nearest significant figure in php
Short form:
$x = $y - $y % pow(10, floor(log10($y)));
So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math.
Round always up to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90)
Round always down to the nearest decimal (1.81 = 1.80, 1.89 = 1.80, 1.85 = 1.80)
Round always up to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 2, 1.32 = 1.50)
Round always down to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 1.75, 1.32 = 1.25)
Round always up to the nearest x.50 / 1 (1.23 = 1.50, 1.83 = 2)
Round always down to the nearest x.50 / 1 (1.23 = 1, 1.83 = 1.50)
I have searched on Google for 2 hours now and the only things that came up were Excel forums. Is it possible with some simple lines of PHP?
Since you're looking for fourths (.00, .25, .50, .75), multiply your number by 4, round to nearest whole number as desired (floor if down, ceil if up), then divide by 4.
1.32, down to nearest fourth:
1.32 * 4 = 5.28
floor(5.28) = 5.00
5.00 / 4 = 1.25
Same principle applies for any other fractions, such as thirds or eighths (.0, .125, .25, .375, .5, .625, .75, .875). For example:
1.77, up to nearest eighth:
1.77 * 8 = 14.16
ceil(14.16) = 15.00
15.00 / 8 = 1.875
Just for fun, you could write a function like this:
function floorToFraction($number, $denominator = 1)
{
$x = $number * $denominator;
$x = floor($x);
$x = $x / $denominator;
return $x;
}
echo floorToFraction(1.82); // 1
echo floorToFraction(1.82, 2); // 1.5
echo floorToFraction(1.82, 3); // 1.6666666666667
echo floorToFraction(1.82, 4); // 1.75
echo floorToFraction(1.82, 9); // 1.7777777777778
echo floorToFraction(1.82, 25); // 1.8
Please note that the answer isn't really water tight. Since we're dealing with floats here it's not guaranteed that when you divide the rounded number by the denominator it returns a neatly round number. It may return 1.499999999999 instead of 1.5. It's the nature of floating point numbers.
Another round is needed before returning the number from the function.
Just in case someone lands here from google like I did :)
According to the mround() function in Excel:
function MRound($num,$parts) {
$res = $num * $parts;
$res = round($res);
return $res /$parts;
}
echo MRound(-1.38,4);//gives -1.5
echo MRound(-1.37,4);//gives -1.25
echo MRound(1.38,4);//gives 1.5
echo MRound(1.37,4);//gives 1.25
Look at example #3 on here and it is half of your solution - http://php.net/manual/en/function.round.php
Is it possible to round a number where if it's .5, just leave it, anything below .5 round down, anything above .5 round up?
For example:
5.0 * 1.35 = 6.75 // leave it
5.2 * 1.35 = 7.02 // round down to 7.00
5.5 * 1.35 = 7.56 // round up to 8.00
I've formatted with round($n,0, PHP_ROUND_HALF_UP) where $n is the product from the above calc , which leaves 6.75 but returns 7.02 for the next one. I also tried round($n,-1, PHP_ROUND_HALF_UP) which gives me the 7.00 on the second calc but then of course won't return a 6.75 for the first, instead it returns 680.
This is a ticket markup calculation where the user enters the first number and is multiplied by the second. I actually remove the decimal because they don't want to see it, and they want this sort of customized rounding on the result.
function myround($num, $prec) {
$rhu = round($num, $prec, PHP_ROUND_HALF_UP);
$rhd = round($num, $prec, PHP_ROUND_HALF_DOWN);
return ($rhu + $rhd) / 2;
}
Works for any precision you like. For hundreth's place, as in the example, $prec would need to be 2.
The only way to determine the value of the last non-zero digit of a given floating point number in PHP is to convert it to a string.
$str = (string) $float;
$result = ($str[strlen($str) - 1] == 5) ? $float : round($float);
Example
Of course, no matter what you do it will be subject to a small margin of error because of the floating point precision issue.
$n = round($n, 2);
if($n % .05 != 0 || $n % .1 == 0)
{
$n = round($n);
}
Does this work for you? I'm assuming the 5 you speak of is the hundredth digit, and if it's not 5 then you want a whole number.
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;
}