This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 8 years ago.
What I'm trying to do seems rather simple, but I can't get it to work with the php.net documentation..
Numbers should always be rounded to "tens"
So:
1 -> 10
7 -> 10
12 -> 20
18 -> 20
23 -> 30
35 -> 40
Something like this should work:
$rounded_num = round($orig_num / 10) * 10;
Just read documentation about round.
round($number, -1);
Your should use round() function.
But if you want manual way, you can use something like that:
$number = 189;
$length = strlen((string)$number);
$div = '1';
for ($i=0;$i<$length-1;$i++){
$div .= '0';
}
$result = (int)$div*(ceil($number/$div));
$result = 200
Related
This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);
This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 3 years ago.
Anyone knows how i can convert How to convert numbers like 902.1 or 902 to 900 in php..i have tried both ceil and floor like below but still same issue
$amount = 902.1
$amount2 = floor($amount);
return $amount2;
returns 902
but i want to return either 900 or 910..something like this
This did it for me
$number = ceil($amount / 10) * 10;
This question already has answers here:
Best way to round down in PHP
(3 answers)
Round up to nearest multiple of five in PHP
(12 answers)
Closed 4 years ago.
How can I round an integer down to the nearest multiple of 3 using PHP? And have <3 be 0.
For example:
4 becomes 3
10 becomes 9
9 becomes 9
8 becomes 6
And so on...
Assuming $x is your input:
print $x-$x%3;
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* #param integer $number
* #param integer $multiple
* #return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
I know there are good answers here but this one is for larger numbers for the sake of alternative, using bcmath.
function floor_to_multiple($number, $multiplier) {
return bcsub($number, bcmod($number, $multiplier));
}
If you want for 3, you may use function below:
function round_nearest_3($value){
return $value-$value%3;
}
If you want to return function for any value use function below:
function round_nearest_mod($value,$mod){
return $value-$value%$mod;
}
Examples:
echo round_nearest_3(4); // becomes 3
echo round_nearest_3(10); // becomes 9
echo round_nearest_3(9); // becomes 9
echo round_nearest_3(8); // becomes 6
echo round_nearest_mod(4,3); // becomes 3
echo round_nearest_mod(10,3); // becomes 9
echo round_nearest_mod(9,3); // becomes 9
echo round_nearest_mod(8,3); // becomes 6
<?php
//requested number
$num = 10;
//calc
for($i=1;($i*3)<=$num;$i++)$answer[] = $i;
$answer = max($answer)*3;
//print result
print_r($answer);
I misread that even if it is a perfect fit, it should round down to the last preceeding incremental:
4 becomes 3
10 becomes 9
9 becomes 6
8 becomes 6
Therefore, if for some reason you need this; your answer would be:
print $x-($x-1)%3-1;
I made a mistake in comprehending the question but thought this answer was curious enough to be worth posting.
This question already has answers here:
Random Float between 0 and 1 in PHP
(13 answers)
Closed 8 years ago.
I've been trying to get this work for almost an hour now, and it's constantly eluding me.
public static function npcBattleStats($npc) {
$npc = self::findNPC($npc);
$rand = mt_rand(1.2, 3.3);
$npcStats['attack'] = (($npc['level'] * $npc['power']) / $rand);
$npcSpeeda = round($npc['power'] / 2.4);
$npcSpeedb = round(($npc['power'] / 1.1) + 2);
$npcStats['speed'] = mt_rand($npcSpeeda, $npcSpeedb);
return $npcStats;
}
level = 3 | power = 50
$npcStats['attack'] = (($npc['level'] * $npc['power']) / $rand);
Everything else seems to work properly, but the attack is constantly off. It should be between 45 and 115 but only returns the following.
[attack] => 50 | [attack] => 150 | [attack] => 75
I hope I'm not missing something simple here, but help would be greatly appreciated.
It seems that the Funktion mt_rand() only spits aut integers, not floats. Try this instead:
$rand = mt_rand(12, 33) / 10;
First of all as cHao mentioned
PHP manual: "Return value: A random integer value between min (or 0) and max (or mt_getrandmax(), inclusive), or FALSE if max is less than min."
To get it fixed you need something like this
$rand = mt_rand(35, 100) / 10;
//35 is your max
//120 is your low
So when You get:
your max:
(5 * 50) / 3.5 = 42.85.. //so round it
your low:
(5 * 50) / 10 = 15
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
Lets say I divide 14 / 15 times it by 100 to get the percentage which is 93.33333333333333 how can I display it as 93.3% using php?
Here is the code.
$percent = ($avg / 15) * 100;
The sprintf function is made for this (see also the manual):
echo sprintf('%.1f%%', $percent);
PHP has a number_format function which lets you specify the number of decimals, what to use for the decimal separator, and what to use for the thousands separator:
$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%