I have a problem with some SQL value & PHP.
I am doing 2 request, one to add a value into my sql and the other one to minus this result.
Here it is :
$totaux = $montant_actuel + ($nbre_dej * $prix_dejeuner) + ($nbre_din * $prix_diner) + ($nbre_soir_etape * $prix_etape);
When i'm doing this, it's equal to:
sum = 0 + (15.8*1) + (15.8*1) + (57.8*0)
So I have : 31.6
But when i'm trying to do :
$totaux = $montant_actuel - ($nbre_dej * $prix_dejeuner) - ($nbre_din * $prix_diner) - ($nbre_soir_etape * $prix_etape);
sum = 31.6 - (15.8*1) - (15.8*1) - (57.8*0)
And then, when i insert it into my DB i have this record : 0.0000000000000142109 instead of 0
I don't understand why is this happening.
it seems like a simple rounding error inside the database. if you're using float or double change it to decimal to avoid rounding errors.
It can be a problem with precision of numbers... use some functions from: here
Related
I was trying to run average in my project but I was not able to make the round function work, every time I run the function I always end up with float value example: average is 86.5, I wanted it to round to nearest tens which is 87 or if lower it will turn 86.
Controller
$score->average =round( $row['result1'] + $row['result2']) /2;
Schema
$table->integer('result1');
$table->integer('result2');
You can get integer value like this:
$score->average = (int)round(($row['result1'] + $row['result2']) /2);
Since, round function returns floating point value like 3.0, 45.0 so to get integer you have to type case the value.
Can you try this one
$score->average =round( ($row['result1'] + $row['result2']) /2) ;
using this will apply the PEMDAS rule.
Hope it helps
You should try this:
$summation = $row['result1'] + $row['result2'];
$divideRslt = $summation / 2;
$score->average =round($divideRslt);
Javascript: var random = Math.exp(Math.random() * Math.log(100 - 10 + 10)) + 10;
Edit: + 10 inside the log function was a typo.
Attempt for a PHP Equivalent: $random = exp(rand() * log(100 - 10 + 10)) + 10;
The latter returns false, where the first returns a number. The problem is definately the exponent function that behaves differently.
How can I fix this?
Math.random returns a number between 0 and 1.
rand() return a big integer. That's where the infinite comes from.
Edit
Documentation for rand() and Math.random
You can change your code as follows to overcome this:
$random = exp((rand(1, 1000)/1000) * log(100 - 10 + 10)) + 10;
Everyday we need to calculate number of units allocated based on two variables called investment amount and NAV. This we are doing now in Excel using rounddown function, which I am trying to achieve in PHP but it is sometimes rounding up the result.
In Excel:
Cell A1 = 287011570.56
Cell B1 = 14.9482
Cell C1 = =ROUNDDOWN(A1/ROUND(B1,4),4)
= 19200410.1202
In PHP:
<?php
$var1=287011570.56;
$var2=14.9482;
$var3=$var1/$var2;
$var4 = floor(($var3) * 100000 + .5) * .00001;
$var5 = intval($var4);
$var6 = strlen($var5)+5;
$test = substr("$var4", 0, $var6);
print "$test";
?>
Result : 19200410.1203
The result should contain 4 digits after decimal and it should not round up the result.
Thanks in advance.
You shouldn't need to add the 0.5 when you are using floor(). That can move the value to the next highest value which you don't want when you are doing division.
From your example just doing:
floor($var3 * 10000) / 10000
Gave me the correct result and should work properly.
Use Round - float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ).
You need to pass PHP_ROUND_HALF_DOWN as 3rd parameter(Mode).
round($value, 4, PHP_ROUND_HALF_DOWN);
If mode parameter is not available in your version of PHP(as it didn't work for me) then use this:
floor($value * 10000) / 10000;
I want to get rows with floats like my $float.
I used this code:
$float = $_GET['float'];
$requst = mysql_fetch_array(mysql_query("SELECT * FROM floats WHERE float LIKE'$float.%%%%%%%'"));
then I echoed all the rows with while:
while ($r = $request) {
echo $a['float'];
}
but the page doesn't show anything. (YES, I typed in the address bar ?float=34 and there are floats like 34.****** in the table.)
What is the problem?
(PHP version 5.2, MYSQL version 5.0)
Try This
$requst = mysql_query("SELECT * FROM floats WHERE float LIKE'$float.%%%%%%%'");
while ($r = mysql_fetch_array($request)) {
echo $r['float'];
}
How about select * from floats where floor(float) = $floor;?
Your variable already has a decimal point in it.
"SELECT * FROM floats WHERE float LIKE '$float%'"
If your float is 34.567 your statement executes as find like '34.567.%' which isn't finding any results.
Edit:
how about this then?
"SELECT * FROM floats WHERE abs(float-$float)<1;"
That will bring in anything within 1?
Having said that, although you can keep floats in mysql, it might be safer to keep them as decimals with a limited number of points after the decimal.
There are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, $1 (100p) and $2 (200p).
It is possible to make $2 in the following way:
1x$1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p
How many different ways can $3 be made using any number of coins?
how can we do it using PHP?
This problem and those like it are best solved using generating functions.
For this case, you want to consider terms of the form
1/(1 - x^k) = 1 + x^k + x^(2k) + x^(3k) + ...
where k is one of the values 1p, 2p, 5p, 10p, 20p, 50p, 100p, 200p. Now multiply all of these 8 terms together to get
f(x) = 1 / [ (1-x) * (1-x^2) * (1-x^5) * ... * (1-x^200) ]
Then the coefficient of x^m is exactly the number of ways mp can be made from the given denominations. For example, the coefficient of x^200 is 6, which corresponds to the fact that there are exactly 6 ways to get 200p = $2 from the given denominations.
Here's a quick and dirty explanation for why this works. The coefficient of x^m in f(x) is the number of ways to take one term of the form x^(i*k) from each linear factor of the form (1 - x^k) in the denominator so that the sum of the exponents is m, i.e.
i1*k1 + i2*k2 + ... + i8*k8 = m
Now the term with k1 = 1 corresponds to taking 1p, the term with k2 = 2 corresponds to taking 2p, the term with k3=5p corresponds to taking 5p, and so on. The sum above becomes
i1*(1p) + i2*(2p) + i3*(5p) + ... + i8*(200p) = m
which gives the amounts to take of each denomination.