I have a question and I don't know what is the correct term to use in search!
what PHP function should I use to print the number like this
Original number > Expected result
15001 > 15000
16300 > 16000
22700 > 22000
I mean to remove all numbers after the thousands
any help ?
Just some math:
Divide the $number by 1000, round fractions down with floor or ceil, then multiply it by 1000.
$number = 22700;
// if ($number < 1000) { /* don't apply this */ }
echo floor($number / 1000) * 1000; // 22000
// or
echo ceil($number / 1000) * 1000; // 23000
One way to rome:
foreach([1,12,123,1234,12345,123456,1234567] as $num){
print $num. " > ". (substr($num,-1*strlen($num),-3)."000") . '<br>';
}
//result:
//1 > 000
//12 > 000
//123 > 000
//1234 > 1000
//12345 > 12000
//123456 > 123000
//1234567 > 1234000
No rounding here. Just replacing.
Related
I have a variable $user_id (number value)
Want to check if $user_id falls in between the range of 1 - 1000 or 1001 - 2000 or 2001 - 3000 .... 99001 - 100000
Is there a way to do this without writing 100 switch or if statements in PHP?
When it finds the match, execute a code.
I know while and for loops are required for this. But not able to code it properly.
This the simplest way:
$check = 0;
$nextCheck = $check+1001;
while ($check < 100001) {
If ($user_id > $check && $user_id < $nextCheck) {
// Code ...
break;
} else {
$check+=1000;
$nextCheck+=1000;
}
}
You can just divide the number by 1000 since you have a 1000 interval range in a consecutive way.
The quotient * 1000 + 1 is your start value for the interval with an exceptional corner case of a number divisible by 1000, which would just be the border end for an interval.
<?php
$tests = [1,999,1000,50001,100000,2999];
foreach($tests as $test_case){
$quotient = intval($test_case / 1000);
if($test_case % 1000 === 0){
$start = $test_case - 1000 + 1;
echo "$test_case : Range: ($start - $test_case)",PHP_EOL;
}else{
$start = $quotient * 1000 + 1;
$end = ($quotient + 1) * 1000;
echo "$test_case : Range: ($start - $end)",PHP_EOL;
}
}
Output:
1 : Range: (1 - 1000)
999 : Range: (1 - 1000)
1000 : Range: (1 - 1000)
50001 : Range: (50001 - 51000)
100000 : Range: (99001 - 100000)
2999 : Range: (2001 - 3000)
Demo: https://3v4l.org/apbqV
Do not generate an array. Do not use a loop. Use math to determine the upper limit of the range, then subtract 999 from that number -- done.
*my snippet assumes we are only dealing with positive values between 1 and 100000.
Code: (Demo)
$tests = [1, 999, 1000, 50001, 100000, 2999];
foreach ($tests as $test) {
$upper = intval(($test - 1) / 1000) * 1000 + 1000;
echo "$test is between " . ($upper - 999) . " and $upper\n";
}
Output:
1 is between 1 and 1000
999 is between 1 and 1000
1000 is between 1 and 1000
50001 is between 50001 and 51000
100000 is between 99001 and 100000
2999 is between 2001 and 3000
Formula Breakdown:
intval( #3) remove decimals from difference
($test - 1) #1) subtract one
/ 1000 #2) divide by 1000
)
* 1000 #4) multiply integer by 1000
+ 1000 #5) add 1000
$num=621;
echo round(round(621,-2));
Something like this (returns the closest $multiple not less than the given $number):
function round_up($num, $mul) {
return ceil($num / $mul) * $mul;
}
Called like this:
echo round_up(621, 100);
> 700
Works for "weird" quantities, too:
echo round_up(124.53, 0.25);
> 124.75
echo round_up(pi(), 1/7);
> 3.1428571428571
If you want to specify decimal places instead of multiples, you could use the power operator ** to convert decimal places to multiples.
You could do round_down in a similar way using floor.
I think you are trying to round off a number to the nearest 100.
Simply do this by using the ceil function.
ceil(621 / 100) * 100;
You can use ceil function to round any number to its nearest number.
$number = ceil($inputNumber / $nearestNumber) * $nearestNumber;
As an option, subtract the reminder and add 100:
function ceil100($value) {
return $value - $value % 100 + 100;
}
My answer is $numero = $numero + 100 - $numero % 100;
php > $numero = 621;
php > $numero = $numero + 100 - $numero % 100;
php > echo $numero;
700
i want to round up a number like this
1439 to 1400
1399 to 1350
What are the nearest way to do this in php?
Given the new examples...
Looks like you want to use PHP floor instead and apply the 50 yourself
50 * floor($number / 50)
OLD ANSWER
Going from your examples, rather than the question title..
Try the PHP round function.
In your case:
round($number, -2);
The second param is the number of decimal figures to round to, negative values go to the left of the 'ones' digit instead.
There is also a third parameter for some more subtle variations.
$rounded_n1 = round($n1 / 50, 0) * 50;
You can do something like that (only to round down) :
$n1 = 1439;
$n2 = 1399;
$round1 = $n1 - $n1 % 50; // round1 = 1439 - 39 = 1400
$round2 = $n2 - $n2 % 50; // round2 = 1399 - 49 = 1350
To round up, you can do this :
$n1 = 1439;
$n2 = 1399;
$round1 = $n1 + (50 - $n1 % 50); // round1 = 1439 + (50 - 39) = 1450
$round2 = $n2 + (50 - $n2 % 50); // round2 = 1399 + (50 - 49) = 1400
You can do it like:
Divide it by 100.
Truncate.
Multiply by 100.
This is the best thing I could come up with.
$num = 1401;
$num /= 100;
$num = round($num);
$num *= 100;
Use ceil to always round up
$round1 = ceil($n1/ 50) * 50
You should try:
function specialRoundUp($val) {
return 100 * round($val / 100);
}
I know ceil, which will round 15.1 to 16 and 31.2 to 32.
But how to round up to the next x20 number? like 15.1 to 20 and 31,2 to 40?
Is needed to make sensefull labels for the y-axis of a chart.
Try this (works in JavaScript):
$result = 20 * ceil($input / 20);
Here, we're rounding to 20. To round to other numbers, simply replace 20 with whatever base you want. The documentation for ceil() can be found here.
A function that does the same:
function roundTo($value, $base)
{
return $base * ceil($value / $base);
}
As a small aside, if you want to round to the nearest base, instead of rounding up, use round() instead of ceil().
divide by 20 and use ceil, then multiply by 20
Or you could use modulus:
echo round($a + 20 - ($a % 20));
If you want "mid-range" values to round more, you can use the following.
function roundTo($n,$i = 1){
$r = $n % $i;
$d = round(($n - $r * $i) / $i);
return $r * $i + $d * $i;
}
for ($a = 0; $a < 100; $a++){
printf("%d = %d\r\n", $a, ceil2($a, 20));
}
The above produces:
Number: Rounded To:
0-10 0
11-30 20
31-50 40
etc.
This more closely simulates how 1.3=1 but 1.5=2.
you can use this
ceil(x/2)*2
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.