Why show incorrect result with php function? [duplicate] - php

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why show incorrect result with php function ?
This is my code. It's must to show 0
But when i test this code. It's show 99
Why ?????
<?PHP
$first = "8.12";
$second = "0.12";
$first = ($first)*(100);
$second = ($second)*(100);
$results = ($first)-($second);
echo ($results)%(100);
?>

This seems to be a weird issue with floating point variables (double). Here's how you can fix it without worrying about casting variables:
$first = "8.12";
$second = "0.12";
$first = ($first)*(100);
$second = ($second)*(100);
$results = round(($first)-($second));
echo ($results)%(100);
Adding round to the results calculation (or, if you want, when multiplying $first and $second with 100) will solve the floating point error.
You can also specify the precision of round() with a second argument, such as
round(123.4567, 2); // 123.46

Related

Is there any way to regex and delete after a certain point in a decimal number in PHP? [duplicate]

This question already has answers here:
PHP How do I round down to two decimal places? [duplicate]
(16 answers)
Closed 4 years ago.
Is there any way to do a regex that cuts off a number at a certain point without rounding (simply drops the digits?) say after 4 digits.... It will not be handling negative numbers, EVER. I could have number inputs such as 0.03123 or 1.31, or 10000.98, etc .... What I have written so far as my solution is rounding and not what I'm seeking....
$number = 10000.51999999;
$precision = 4;
echo "<br>";
// grab number before decimal by rounding down the whole number down...
$numberBeforeDecimal = floor($number);
echo "<br>";
// grab the decimal and set the correct precision needed
$n = $number;
intval($n); // 12
$theDecimalPart = explode('.', number_format($n, ($precision)))[1]; // 3430
echo $theDecimalPart; // this is outputting 5200
$theNewValue = $numberBeforeDecimal.".".$theDecimalPart;
explode() the number to get integer and decimal part separated out in an array
Use substr() function to get relevant precision from the decimal part.
Finally, concatenate them back.
Try the following (Rextester DEMO):
$number = 10000.51999999;
$precision = 4;
// separate out the integer and decimal part
$number_str_arr = explode('.', $number);
// concatenate them back
$theNewValue = $number_str_arr[0] . '.' . substr($number_str_arr[1], 0, $precision);
echo $theNewValue; // displays 10000.5199

Why does PHP think that 3.57 + 0.01 equals 3.5799999999999996? [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 4 years ago.
Somehow PHP thinks that 3.57 + 0.01 equals 3.5799999999999996 in my script.
I tested about 10 other sums like this (e.g. 10.51 + 0.01) and then PHP gave me the correct answer (10.52 in the latter case).
So, weirdly enough it only seems to make this mistake with these specific floats.
Code: (simplified for readability)
$users = array();
$IDs = [1,2,3];
foreach($IDs as $ID)
{
$user = (object) array();
$user->ID = $ID;
$users[] = $user;
}
$initialAmount = 3.57;
$chosenIDs = [1,3];
foreach ($users as $key => $value)
{
$users[$key]->amount = $initialAmount;
if(in_array($key, $chosenIDs))
{
//the following returns 3.5799999999999996
$users[$key]->amount = $users[$key]->amount + 0.01;
//even if I do it like the following, it returns the wrong answer
$users[$key]->amount = 3.57 + 0.01; //according to PHP equals 3.5799999999999996
//the following actually gives the correct answer though
//$users[$key]->amount = ($users[$key]->amount * 100 + 1) / 100;
}
}
Is this some weird error in my configuration, or what is going on here?
As others have mentioned, it's a problem with Floating Point Math, not php.
http://php.net/manual/en/language.types.float.php
You can avoid it by using the BC Math extension when working with numbers where Floating Point errors would be a problem. It supports arbitrary precision.
http://php.net/manual/en/ref.bc.php

Need one digit after decimal without changing any number [duplicate]

This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);

Invalid value when using floats [duplicate]

This question already has answers here:
PHP - Floating Number Precision [duplicate]
(8 answers)
Closed 9 years ago.
The following operation give out the wrong result.
$First = '45.4000';
$Second = '4.6800000000';
$Third = '50.00';
echo ( $First + $Second ) - $Third;
OUTPUT: 0.079999999999998
Expected Output: 0.08
I am looking on how to get the right result, without using number_format/sprintf ...etc.
As this issue is affecting multiple places in my code & have to go over everything & formatting it is a pain.
As a "quick fix", change the precision setting in your php.ini file. Documentation.
By default, it is 14, which is more than you need almost all the time (and if you need that much precision you'd be using a dedicated math library). Change it to something like 4, and the result will be rounded to that length - note that you can still override this with number_format on a case-by-case basis if you need to.
Try This
$First = '45.4000';
$Second = '4.6800000000';
$Third = '50.00';
$sk = ( $First + $Second ) - $Third;
echo round($sk,4);
?>

PHP round different results [duplicate]

This question already has answers here:
PHP Rounding Numbers
(6 answers)
Closed 8 years ago.
Hi i got some problem with rounding. For ex.:
$x = 100;
$y = 4.2030;
$result = round($x / $y, 2);
$result will be 23.79
but now
$result2 = round(23.79 * 4.2030, 2);
$result2 will be 99.99 , so it's incorrect. should be 100 ($result2 equal $X)
how to slove it ?
Your round precision is two decimal places. If you are trying to get whole numbers you need to omit the precision argument:
$result2 = round(23.79 * 4.2030);
NOTE: the lower the precision argument, the more inaccurate your result will be from the actual results.
You can also use ceil() and floor() if you are looking to round in a specific direction (ceil() will round up, floor() will round down).

Categories