Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm working on a program in PHP that lets you train for making calculations without a calculator. The problem I run into is that when generating random numbers for the sums you can get awkward numbers like 4 divided by 7. Is there an easy way to generate easy sums like 9 divided by 3?
Thanks!
The easiest method is to generate two random numbers to multiply together. Get the answer to the multiplication problem. Then use the answer along with one of your original random numbers for your division problem. The answer will always be the second random number you generated.
Pseudo code:
$random1 = random(1, 100);
$random2 = random(1, 100);
$answer = $random1 * $random2;
print("What is $answer / $random1?"); // answer is always $random2
You don't explain what range of random numbers you are using, but be careful not to pick 0 for $random1.
suppose there are two numbers you get at random $a and $b. If u want to divide $a by $b check if $a>$b and then check if remainder is zero. $a%$b==0; if yes you can echo the values.
I've not tested this code, but it should check if the division is easy or not, using the modulo character %, and call the function again if it is not easy. If the division is easy, it returns the numbers as an array.
function easyDivide() {
// Code for generating random numbers
if($num1 % $num2 != 0) {
return easyDivide();
} else {
return array($num1, $num2);
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to find a way to turn convert 36.901775282237 to 36.91 in php which function do I use in order to convert to this value. I've tried the round function but it's not giving me the right value no matter what flag I use.
The normal way is to use number_format
<?php
$newvalue=number_format(36.901775282237,2);
echo $newvalue;
?>
But if you wish to do what you want , then use a few lines of codes. (I think no need to further explain, just simple math)
<?php
$value=36.901775282237;
//$value=36.900775282237; for this case the result will be 36.90
///// do the trick
$value=intval($value * 1000)/1000;
$parsex=number_format($value * 100,2, ".", "");
$parsex=ceil($parsex)/100;
//// end
echo $parsex;
?>
Mathematically speaking, 36.901775282237 doesn't round up to 36.91 but rather rounds down to 36.90. You'll need to combine ceil with some custom math to get the rounding you're asking for. If you're looking for mathematically correct rounding, number_format will accomplish that internally:
function ceilWithPercision($number, $decimalPoint) {
$number = $number * pow(10, $decimalPoint);
$number = ceil($number);
$number = $number / pow(10, $decimalPoint);
return (string) $number;
}
$number = 36.901775282237;
var_dump(ceilWithPercision($number, 2)); // "36.91"
var_dump(number_format($number, 2)); // "36.90"
Example here: https://3v4l.org/NMOpH
You should take a look at some of the Php Documentation, and maybe some tutorials online before asking these kinds of questions here.
What I think you are looking for is number_format(36.901775282237, 2)
This question already has answers here:
PHP Gives Me Wrong Result Of Subtraction With Floating Number [duplicate]
(2 answers)
Closed 2 years ago.
As a computer engineer I am totally aware of how floating point arithmetic works on the CPU (ALU) layer. This is NOT my question. My question is how to accurately subtract two floating point values without having to write hundreds of lines of boilerplate code to do so. In a script language like PHP there MUST be a simple way to do this, right? I mean we are living in the year 2020 and still have to deal with this kind of crap on the software side? Excuse my language.
I have two floats (same values) - one is retrieved from a database and the other one is retrieved via JSON request. Printing the two values with echo results in:
$a = 1865183.4082;
$b = 1865183.408200000000000000;
Now I want to subtract them and obtain the true result (precision of 10 decimals) which is zero. I tried the following three ways:
$res = $a - $b;
// $res = 7.4505805969238E-9
$res = floatval(number_format($a, 10, ".", "")) - floatval(number_format($b, 10, ".", ""));
// $res = 7.4505805969238E-9
$res = bcsub(number_format($a, 10, ".", ""), number_format($b, 10, ".", ""), 10);
// $res = 0.0000000075
I just spent two hours on finding the solution. No success. Please help.
EDIT: Someone closed this question as if I wasn't able to look at all the other questions related to this. My problem is NOT solved and there is no solution in the linked question. Thanks.
Please round the result:
$a = 1865183.4082;
$b = 1865183.408200000000000000;
echo $result = round($a - $b, 2);
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 3 years ago.
I'm trying to show one decimal place for rating number I have
but it returns unexpected value.
I used number_format and the round functions and both have the same issue, or i'm doing something wrong.
I tried to make this number show one decimal number
4.96 and it always returns 5 instead of 4.9
number_format(4.96, 1)
round(4.96, 1)
round(4.96, 1,PHP_ROUND_HALF_DOWN)
both functions returns 5 instead of 4.9
I searched all answers but couldn't find anything helpful.
Rounding 4.96 will round .9 up, so it will be 5 in all cases. If you want to do it without rounding, you may have to tweak it a bit to fool it:
floor(4.96 * 10) / 10; // 4.9
Here's a function you can use to achieve this.
function convertToSingleDecimal($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
print convertToSingleDecimal("4.96", 1);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example if I had:
1.2
1.65
5
9.5
125
Valid numbers would be 5 and 125.
Invalid numbers : 1.2, 1.65, 9.5
I am stuck in checking whether a number has a decimal or not.
I tried is_numeric but it accepted numbers with a decimal.
A possibility is to try using strpos:
if (strpos($a,'.') === false) { // or comma can be included as well
echo 'Valid';
}
or try it using regex:
if (preg_match('/^\d+$/',$a))
echo 'Valid';
Examples taken from here.
If you are sure the number variable is not a string you can use is_int() to check whether it is valid or is_float() to check whether it is invalid.
But if you handle forms for example the variable is often a string which makes it harder and this is an easy solution which works on strings, integers and floats:
if (is_numeric($number)) {
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer!!
}
}
It's also faster than regex and string methods.
Use is_float() function to find invalid numbers
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.