i want to add zero after number if number is single
e.g $a = 2 then 20
if $a = 20 then 20
if $a = 12 then '12'
like this. I some things but it didn't work for me.
How to do it ?
Use if-statements:
if ($a < 10) $a *= 10;
That shouldn't be too hard.
$number = intval($number)
if ($number > 0 && $number < 10) {
$number *= 10;
}
Use str_pad to pad with zeros.
http://php.net/manual/en/function.str-pad.php
Echo str_pad($a, 2, "0", STR_PAD_RIGHT);
Related
I know the following code will remove the numbers are the point.
round($number);
I want to round the numbers as follows
if number is 20.123
I want result 20,
If number is 20.567
I want result 21
Means if value is below .5 , it should remove that value.
If value if .5 or above it should round up.
How ?
Anyone help ?
round($number) will do what you want:
round(20.156); // 20
round(20.651); // 21
Live example
I am sure it's working well.
<?php
$var = 22.443;
$var = number_format($var, 0, '.', '');
echo $var;
?>
function get_decimal_num($number){
$num = explode('.', $number);
return $num[1];
}
$num = 10.5 ; // for example
$val = get_decimal_num($num);
if($val >= 5)
{
$value = (int) $num;
echo $value = $value + 1;
}
if($val < 5)
{
echo $num;
}
Plese check it...
Try this:
use the ceil, floor, explode and substr function to achieve you value.
$num = "20.123";
$arr = explode(".", $num);
if(substr($arr[1], 0, 1) >= 5){
$num = ceil($num);
}else{
$num = floor($num);
}
echo $num;
Result:
20
Also you can use the round function.
round(20.156); // 20
round(20.651); // 21
I want to get 3 int values in a for loop connected with each other. What I basically want is this:
000
001
002
003
010
011
...
323
330
331
I want to have each of the 3 numbers in a variable starting from 0 and max 3 when it gets higher than 3 it increases the number left to it by 1
For example
for($i = 0; $i <= $array; $i++){
echo $a . $b . $c . "<br />"; //Output would be the example I showed above
}
You can use base_convert to convert between arbitrary bases, in your case base 4.
for($i=0, $max = base_convert(333, 4, 10); $i < $max; ++$i) {
echo base_convert($i, 10 , 4);
}
To get 0-padded output, use printf with a format specifier:
printf('%03d', base_convert($i, 10 , 4));
for($i = 0; $i <= 63; $i++){
$c = $i % 4;
$b = ($i - $c)/4 % 4;
$a = (($i - $c)/4 - $b)/4;
echo $a . $b . $c . "<br />"; //Output would be the example I showed above
}
It's really just an explicit version of knittl's answer. The for loop has us step through every number from 0 thru 63, which happens to correspond with 0 through 333 in base 4. We then take $i, which is in base 10, and convert it to base 4 step by step. The % is the modulo operator - it returns the remainder after division. The least significant char is simply the remainder of $i/4, so we save that as $c. The next char is the 4 place (like the 10 place in base 10). So we subtract $c, which is already accounted for and divide by 4 and do the same thing.
for($a = 0; $a <= 3; $a++){
for($b = 0; $b <= 3; $b++){
for($c = 0; $c <= 3; $c++){
echo $a . $b . $c . "<br />";
}
}
}
Try this:
$count = 20; //how many numbers do you want
for($i =0; $i<$count; $i++) {
echo str_pad(base_convert($i, 10, 4),3,'0' , STR_PAD_LEFT) . '<br/>';
}
base_convert() converts every $i value from 10 base to 4.
str_pad() fill it with '0' to given length (3 here).
STR_PAD_LEFT means that zeros should be added on left side.
I'm wondering how I would go about the addition and subtraction of numbers in a set range and which would loop back on themselves, example below;
Range: 1 - 10
So if I now had the number 7 and added 5 to it, I would want the number go to 2
8, 9, 10, loop around to 1, 2.
And the same if I subtracted, so I have the number 3 and I subtract 4 so I should be left with 9.
2, 1, loop around to 10, 9
I hope this makes sense.
Thanks.
You can use % operator.
It calculates remainder after division.
For example:
$d = 10;
$x = 7;
$y = 5;
echo ($x + $y) % $d;
gives 2;
With negative values you can just remove MINUS
Use the modulus operator.
result = (a + b) % 10;
You can use modulo function like (7+5)%10 = 2
Try this:
$range = range(1,10);
$min = min($range);
$max = max($range);
function operate( $a, $b, $operation ) {
global $max, $min;
switch( $operation ) {
case '+':
$a += $b;
break;
case '-':
$a -= $b;
break;
}
if( $a < $min ) {
return $max + $a;
} else if( $a > $max ) {
return $a - $max;
}
}
Hope it helps.
You can do this with code like
$range = array('from' => 3, 'to' => 13);
$dist = $range['to'] - $range['from'];
$a = 7;
$b = 14;
echo ($dist + ($a % $range['to'] - $b % $range['to'])) % $dist; // $a - $b
echo ($dist + ($a % $range['to'] + $b % $range['to'])) % $dist; // $a + $b
Modulo will do the trick as others have shown, but you must also account for the lower end of the range.
E.g. looping an arbitrary value within an hour range will work since it's zero-based. But if you want to loop a value within a month range you will get into trouble with the last day, because:
31 % 31 = 0
So you will loop to zero when you should remain on 31.
To deal with any range, you need to do this:
$min = 5;
$max = 15;
$value = 25; // The range is 11, so we want this turned into 14
$range = $max - $min + 1;
$value = (($value-$min) % $range) + $min;
To deal with values below minimum:
$range = $max - $min + 1;
$value = ($min - $value) % $range;
$value = $max - ($value - 1);
I have a simple round function. It rounds to an even number. I want to make sure that number is divisible by 16. Anyone know an easy way to round the number to the nearest number divisible evenly by 16?
$num=round(480/$other_num); //will output some number.
$num = 39;
$num = round($num / 16) * 16; // 32
Perhaps you can bit-and the number with 0xf and add 1?
the easy way seems to be divide by 16, then use the "classical round" and multiply back by 16.
$num=round(480/16)*16;
function round16($num) {
if ($num % 16 == 0) return $num;
$num2 = $num;
$remain = 0;
do {
$remain = --$num % 16;
} while ($remain != 0 && $num >= $num2 - 7);
if ($remain == 0) return $num;
do {
$remain = ++$num2 % 16;
} while ($remain != 0);
return $num2;
}
Probably not the most efficient way to do it.
I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
how can I do that ?
if ($number % 6 != 0) {
$number += 6 - ($number % 6);
}
The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.
If decreasing is acceptable then this is even faster:
$number -= $number % 6;
if ($variable % 6 == 0) {
echo 'This number is divisible by 6.';
}:
Make divisible by 6:
$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
$num += (6-$num%6)%6;
no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.
So you want the next multiple of 6, is that it?
You can divide your number by 6, then ceil it, and multiply it again:
$answer = ceil($foo / 6) * 6;
I see some of the other answers calling the modulo twice.
My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.
Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.
Code: (Demo)
$factor = 6;
for($x = 0; $x < 10; ++$x){ // battery of 10 tests
$number = rand( 0 , 100 );
echo "Number: $number Becomes: ";
if( $remainder = $number % $factor ) { // if not zero
$number += $factor - $remainder; // use cached $remainder instead of calculating again
}
echo "$number\n";
}
Possible Output:
Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
Use the Mod % (modulus) operator
if ($x % 6 == 0) return 1;
function nearest_multiple_of_6($x) {
if ($x % 6 == 0) return $x;
return (($x / 6) + 1) * 6;
}
Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.
while ($number % 6 != 0) {
$number++;
}
Assuming $foo is an integer:
$answer = (int) (floor(($foo + 5) / 6) * 6)
For micro-optimisation freaks:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
Why don't you use the Modulus Operator?
Try this:
while ($s % 6 != 0) $s++;
Or is this what you meant?
<?
$s= <some_number>;
$k= $s % 6;
if($k !=0) $s=$s+6-$k;
?>
result = initial number + (6 - initial number % 6)