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
Related
issue with converting number of product to dozen.
I have 40 products and I want to convert it into dozen.
when I use this calculation 40 / 12 = 3.3333333333333335 and number_format to 1 decimal it returns 3.3 but it means 39 products
so I want that if number after decimal are more than 2 i.e 3.33333 then it round to greater number after decimal that is 3.4
How can I convert that?
We can custom function round_up like:
function round_up($number, $precision = 2)
{
$fig = pow(10, $precision);
return (ceil($number * $fig) / $fig);
}
var_dump(round_up(3.3333333, 2));
I want to generate 6 digit numbers.
Now this work great BUT occasionally it generates 4 digit numbers. Not often but some times it does. Why??
$num = rand(000000, 999999);
$num = rand(100000, 999999);
Maybe this do the job :)
If you want to generate numbers from 000000 to 999999 with 6-digit padding, you can use the str_pad function.
$rand = rand(0, 999999);
echo str_pad($rand, 6, '0', STR_PAD_LEFT);
rand(000000, 999999) is equal to rand(0, 999999)
It will return a number between 0 and 999999. In 90% of all cases the number is between 100000 and 999999 and you will have a 6 digit number. That is why it works for you most of the time
But in 10% of all cases, the number will be smaller than 100000 and only contain 1 to 5 digits (all numbers between 1 and 99999..not hard to figure out that 1 or 2 digits are still less propable then 4 or 5 digits)
To solve your problem you have to get a number from rand(100000, 999999), but this won't contain any numbers starting with 0! The first digit will always be from 1 and 9.
The other answers already show nice solutions for getting 6 digits from 0 to 9. Another easy one would just be:
for($i = 0; i < 6; i++)
$rand_digit[$i] = rand(0,9);
As everyone else said, you could change $num = rand(000000, 999999); to $num = rand(100000, 999999);, but there might be a case where you need a number that has 6 digits, but whose value is below 100000. Ex. 001103. You can still use $num = rand(000000, 999999); but you would use something like:
$num = rand(000000, 999999);
$print_num = sprintf("%06d", $num);
This would not change the number, it will only give it a 6 digit format.
http://php.net/manual/en/function.rand.php
Note :
Warning
min max range must be within the range getrandmax(). i.e. (max - min) <= getrandmax() Otherwise, rand() may return poor-quality random numbers.
So, an other note :
Note: On some platforms (such as Windows), getrandmax() is only 32767. If you require a range larger than 32767, specifying min and max will allow you to create a range larger than this, or consider using mt_rand() instead.
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 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