This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
Adding fractions number yields different result in PHP
$grand_total = (float)$subtotal_email + (float)$delivery_email + (float)$fuel_surcharge_email - (float)$discount_coupon_email + (float)$texas_tax_email - (float)$cancel_fee_email - (float)$refund_email - (float)$refund_tax_email - (float)$coupon_tmp;
echo (float)$subtotal_email." + ".(float)$delivery_email." + ".(float)$fuel_surcharge_email." - ".(float)$discount_coupon_email." + ".(float)$texas_tax_email." - ".(float)$cancel_fee_email." - ".(float)$refund_email." - ".(float)$refund_tax_email." - ".(float)$coupon_tmp." = ".(float)$grand_total;
When I run the above in php, I get the following output:
89.99 + 0 + 16.2 - 0 + 8.61 - 3 - 100 - 10 - 1.8 = -2.88657986403E-15
But if you look at LHS, it should be 0, and this happens with or without float....any idea why?
Floating point arithmetic is never that accurate. If you need to compare to zero, you need to take the different and compare it to some small number.
if (abs($result) < 0.00001)) {
// it's zero
} else {
}
Because float. Use ints and calculate the value with cents (* 100).
Related
I am learning to work with some math like PHP query and just got to the modulo, I am not quite sure in what situations to use this because of something i stumbled on and yes I did already read one of the posts here about the modulo :
Understanding The Modulus Operator %
(This explanation is only for positive numbers since it depends on the language otherwise)
The quote above is in the top answer there. But if I focus on PHP only and i use the modulo like this:
$x = 8;
$y = 10;
$z = $x % $y;
echo $z; // this outputs 8 and I semi know why.
Calculation: (8/10) 0 //times does 10 fit in 8.
0 * 10 = 0 //So this is the number that has to be taken off of the 8
8 - 0 = 8 //<-- answer
Calculation 2: (3.2/2.4) 1 //times does this fit
1 * 2.4 = 2.4 //So this is the number that has to be taken off of the 3.2
3.2 - 2.4 = 0.8 // but returns 1?
So my question is why does this exactly happen. my guess would be that in the first phase it would get 8/10 = 0,8 but this doesn't happen. So can someone explain a bit about why this happens. I understand the modulo's basics like if I do 10 % 8 = 2 and I semi understand why it doesn't return something like this: 8 % 10 = -2.
Also, is there a way to modify how the modulo works? so it would return a - value or a decimal value in the calculation? or would I need to use something else for this
Little shortened: why does this happen when I get a negative number in return and is there some other way or operator that can actually do the same and get in the negative numbers.
Modulus (%) only works for integers, so your calculation at the bottom of your example is correct...
8/10 = 0 ( integer only ), remainder = 8-(0*10) = 8.
If you instead had -ve 12 - -12%10...
-12/10 = -1 (again integer only), remainder = -12 - (10*-1) = -2
For floats - you can use fmod(http://php.net/manual/en/function.fmod.php)
<?php
$x = 5.7;
$y = 1.3;
$r = fmod($x, $y);
// $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7
(Example from manual)
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I'm new to python, I should convert the following operation in php:
if len (s) == 88:
return s[48] + s[81:67: -1] + s[82] + s[66:62: -1] + s[85] + s[61:48 : -1] + s[67] + s[47:12: -1] + s[3] + s[11:3: -1] + s[2] + s[12]
I do not understand exactly what you mean this formatting s[a:b: -c].
s = "012345678910"
a = 7
b = 4
c = 1
print s[a:b: -c]
returns:
765
So to explain, its a slice between position 7 in the string and position 4, first element is element 0, in steps of minus one. That is to say, you get every element between 7 and 4 not including 4.
The general form of a slice as detailed in the docs is slice(start, stop[, step]).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Practices to limit floating point accuracy problems
When I am adding a list of new totalSubs to a running total, PHP returns a weird value.
I have generated the below output (see line 40):
1 The total currently '0' (integer) + new TotalSub '-26969.55' type(double) = total '-26969.55'
2 The total currently '-26969.55' (double) + new TotalSub '249.6' type(double) = total '-26730.05'
...
39 The total currently '-164.89' (double) + new TotalSub '61.95' type(double) = total '-112.94'
40 The total currently '-102.94' (double) + new TotalSub '98.71' type(double) = total '-5.3300000000009'
41 The total currently '-5.3300000000009' (double) + new TotalSub '50' type(double) = total '45.769999999999'
The PHP generating is:
echo ($count++) . " The total currently '$totalTrans' (".gettype($totalTrans).") + new TotalSub '$totalVar' type(".gettype($totalVar).") = total '" . ($totalTrans + $totalVar) ."'<br />";
How can I fix the 00000000009?
From what I can see.
Try multiplying the input data by 100 and then dividing the totals by 100 at the end.
Multiplting by 1 or 10 will most likely not correct it which might be due to the 2 decimals and by using 100 the values are now whole numbers (ints)
Remember floating-point data types (such as DOUBLE) do not represent exact numbers and are approximate.
You could use bcmath() for maths calculations in the future as using number_format() is no different than using round() without returning it as a string.
http://php.net/manual/en/book.bc.php
Use printf to solve that problem and make your code presentable at the same time:
printf("%d The total currently '%.2f' (%s) + new TotalSub '%.2f' (%s) = total (%.2f)<br />",
$count++,
$totalTrans,
gettype($totalTrans),
$totalVal,
gettype($totalVar),
$totalTrans + $totalVar);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP negatives keep adding
I have this code here....
$remaining = 0;
foreach($array as $value=>$row){
$remaining = $remaining + $row['remainingbalance'];
}
What its doing is that it is going through all the remaining balances in the array which are -51.75 and -17.85 with the code above I get -69.60 which is correct. But I am wondering how when its two negatives if they could subtract? Is that possible?
I tried this
$remaining = 0;
foreach($clientArrayInvoice as $value=>$row){
$remaining = $remaining + abs($row['remainingbalance']);
}
but it gives me 69.60 without the negative.
Anyone got any ideas?
my goal is to take -51.75 and -17.85 and come up with -33.90 only when its a negative to do subtract. otherwise add
Whenever you add a negative number, you actually subtract the positive value (and the other way around).
So 0 + (-16) = 0 - 16 = -16.
When you call abs() you calculate something completely different.
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%