Echoing text between numbers with decimals - php

I've putted a script together a bit with array's and stuff.
Now in the end, it checks for a score with checkboxes and divides it by ten.
Then with number format i round it off two 2 decimals, it looks like this.
$number = $score / $total;
$number = $number * 10;
$number = number_format(round($number,2),2,',','.');
echo "The number is: $number <br/>";
Then later on, i'm doing this.
if($number < 4 && $number > 0)
echo 'You're number is between zero and 4';
else if($number > 6 && $number < 4)
echo ' You're number is between 4and 6';
else if($number < 8 && $number > 6)
echo' You're number is between 6 and 8';
else if($number < 10 && $number > 8)
You're number is between 8 and 10';
Now, if the number is like, 0.50 or 1.50 or 2 or 5.50 it's shows the text i've provided.
However, if the number is like 4,13 or 7,85 or 9,13 it doesn't.
Searched now quite a while but can't figure it out.
Do you people see a solution?
Hope i was clear enough!
Thanks in advance.

Is this what you're looking for?
<?php
$score = 7; //Example values
$total = 32;
$number = $score / $total;
$number = $number * 10;
$number = round($number,2);
echo 'The number is: ',number_format($number,2,'.',','),' <br/>';
if($number < 4 && $number > 0)
echo 'Your number is between zero and 4';
else if($number < 6 && $number > 4)
echo 'Your number is between 4 and 6';
else if($number < 8 && $number > 6)
echo 'Your number is between 6 and 8';
else if($number < 10 && $number > 8)
echo 'Your number is between 8 and 10';
?>

The following statement will never validate true:
if($number > 6 && $number < 4)
Do you know any number that's higher than six and smaller than four?
The code also contains numerous syntax errors, for example:
echo 'You're number is between zero and 4';
The apostrophe needs to be escaped using a backslash:
echo 'You\'re number is between zero and 4';
Or use double quotes for strings containing apostrophes:
echo "You're number is between zero and 4";
Finally, grammar whise, you're should be your :)

Related

If number is not multiple of 6 find remainder in PHP

Okay i will have a number from my database. This could be 3, 15 , 138 etc. Basically any number.
Now if the number is not a multiple of 6 i want to find out how many more until it becomes a multiple of 6.
So for instance if my number is 4, i want it to say you need 2 more to reach a multiple of 6.
How can i achieve this? Also when giving an answer could you please explain how the formula works.
I have tried this which someone suggested
$number = 4;
if($number % 6 != 0) {
echo $number += 6 - ($number % 6);
}
But this just prints 6 out
Using += modifies $number by the value returned
echo $number += 6 - ($number % 6);
Results in: 4 += 6 - 4 or $number = 6
Should be
echo $number = 6 - ($number % 6);
Results in: $number = 6 - 4 or $number = 2
Your numbers are backwards:
$number = 4;
if(6 % $number != 0) {
echo (6 % $number) - 6;
}
https://3v4l.org/1V5Rb

Divide an amout equally beteen no of user

I want to distribute an amount among some users equally.If amount is not divisible equally then all other member will get equal amount expect the last member who will get rest of the money.Below is what i have tried
$number = $_POST['number'];
$noOfTime = $_POST['no_of_time'];
$perHead = ceil($number / $noOfTime);
for ($i = 1; $i <= $noOfTime; $i++) {
if ($i == $noOfTime) {
echo $perHead * $noOfTime - $number;
} else {
echo $perHead;
}
}
Here if number is 7 and member are 4 the first 3 member will the 2 and the last will get 1. Like 2,2,2,1.
But this logic seems to be not working for all cases.
Please help.Thanks.
I think it should help you.
$no = 22;
$users = 8;
// count from 0 to $users number
for ($i=0;$i<$users;$i++)
// if the counting reaches the last user AND $no/$users rests other than 0...
if ($i == $users-1 && $no % $users !== 0) {
// do the math rounding fractions down with floor and add the rest!
echo floor($no / $users) + ($no % $users);
} else {
// else, just do the math and round it down.
echo floor($no / $users)." ";
}
OUTPUTS:
2 2 2 2 2 2 2 8
EDIT: I nested an if verification so the logic won't fail even if users are 1 or 2. And since it received more upvotes, I commented the code to make it more clear.

How can I round a number to the nearest number evenly divisible by 16?

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.

Get the sum of all digits in a numeric string

How do I find the sum of all the digits in a number in PHP?
array_sum(str_split($number));
This assumes the number is positive (or, more accurately, that the conversion of $number into a string generates only digits).
Artefactos method is obviously unbeatable, but here an version how one could do it "manually":
$number = 1234567890;
$sum = 0;
do {
$sum += $number % 10;
}
while ($number = (int) ($number / 10));
This is actually faster than Artefactos method (at least for 1234567890), because it saves two function calls.
Another way, not so fast, not single line simple
<?php
$n = 123;
$nstr = $n . "";
$sum = 0;
for ($i = 0; $i < strlen($nstr); ++$i)
{
$sum += $nstr[$i];
}
echo $sum;
?>
It also assumes the number is positive.
function addDigits($num) {
if ($num % 9 == 0 && $num > 0) {
return 9;
} else {
return $num % 9;
}
}
only O(n)
at LeetCode submit result:
Runtime: 4 ms, faster than 92.86% of PHP online submissions for Add Digits.
Memory Usage: 14.3 MB, less than 100.00% of PHP online submissions for Add Digits.
<?php
// PHP program to calculate the sum of digits
function sum($num) {
$sum = 0;
for ($i = 0; $i < strlen($num); $i++){
$sum += $num[$i];
}
return $sum;
}
// Driver Code
$num = "925";
echo sum($num);
?>
Result will be 9+2+5 = 16
Try the following code:
<?php
$num = 525;
$sum = 0;
while ($num > 0)
{
$sum= $sum + ($num % 10);
$num= $num / 10;
}
echo "Summation=" . $sum;
?>
If interested with regex:
array_sum(preg_split("//", $number));
<?php
echo"----Sum of digit using php----";
echo"<br/ >";
$num=98765;
$sum=0;
$rem=0;
for($i=0;$i<=$num;$i++)
{
$rem=$num%10;
$sum=$sum+$rem;
$num=$num/10;
}
echo "The sum of digit 98765 is ".$sum;
?>
-----------------Output-------------
----Sum of digit using php----
The sum of digit 98765 is 35
// math before code
// base of digit sums is 9
// the product of all numbers multiplied by 9 equals 9 as digit sum
$nr = 58821.5712; // any number
// Initiallization
$d = array();
$d = explode(".",$nr); // cut decimal digits
$fl = strlen($d[1]); // count decimal digits
$pow = pow(10 ,$fl); // power up for integer
$nr = $nr * $pow; // make float become integer
// The Code
$ds = $nr % 9; // modulo of 9
if($ds == 0) $ds=9; // cancel out zeros
echo $ds;
Assume you want to find the sum of the digits of a number say 2395 the simplest solution would be to first split the digits and find out the sum then concatenate all the numbers into one single number.
<?php
$number=2;
$number1=3;
$number2=9;
$number3=5;
$combine=$number.$number1.$number2.$number3;
$sum=$number+$number1+$number2+$number3;
echo "The sum of $combine is $sum";
?>
One way of getting sum of digit however this is a slowest route.
$n=123;
while(($n=$n-9)>9);
echo "n: $n";
<html>
<head>
<title>detail</title>
</head>
<body>
<?php
$n = 123;
$sum=0; $n1=0;
for ($i =0; $i<=strlen($n);$i++)
{
$n1=$n%10;
$sum += $n1;
$n=$n/10;
}
echo $sum;
?>
</body>
</html>
Here's the code.. Please try this
<?php
$d=0;
$num=12345;
$temp=$num;
$sum=0;
while($temp>1)
{
$temp=$temp/10;
$d++;
}
echo "Digits Are : $d </br>";
for (;$num>1;)
{
$d=$num%10;
$num=$num/10;
$sum=$sum+$d;
}
echo "Sum of Digits is : $sum";
?>

checking if a number is divisible by 6 PHP

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)

Categories