TLDR: ceil(49) is resulting in 50.
I have a code that reads a CSV file from our provider for updating our database's stock and prices.
I need to get the real product cost, but our price comes in a column, and the discount applied comes into another column, so I have to calculate the original price without discount. And here comes the weird thing.
The discount comes as "0.20" for a 20% discount.
This is the code I use:
while($line = fgetcsv($file, 0, ";")) {
$currentSku = $line[$skuPosition];
$currentPrice = $line[$pricePosition];
$currentDiscount = $line[$discountPosition];
// Only ovewrite price if a discount was applied to the price
if(floatval($currentDiscount)!=0.0 && $currentDiscount!="") {
// Convert 0.20 to 20
$currentDiscount*= 100;
// Replace 45,50 to 45.50
$currentPrice = str_replace(",", ".", $currentPrice);
// Original price without the discount
$currentPrice = ($currentPrice * 100) / (100 - $currentDiscount);
}
// Round up price
$currentPrice = ceil($currentPrice);
// Generate sale price
$currentPrice = $currentPrice * $priceMultiplier;
...
We have a product that costs 39.20, and has a 20% discount applied (0.20 column value).
The price without discount is 49, and the final sale price is 49*2 = 98.
To make sure all values are correct, you make a simple calculation:
If 49 is the 100% of the price... X is the 20% to discount...
So: 20 * 49 = 980
980 / 100 = 9.8
So 9.8 is 20% of 49.
49 - 9.8 = 39.2
39.2 is the price listed on the provider's CSV.
Now, what is the final $currentPrice value of the code shown above? 100
It is calculating 50 * 2 = 100
Now I placed some debugging code to see what's going on, so here we go again: ('762.SSUBGMTVE' is the SKU of the product being proccessed)
while($line = fgetcsv($file, 0, ";")) {
$currentSku = $line[$skuPosition];
$currentPrice = $line[$pricePosition];
$currentDiscount = $line[$discountPosition];
if(floatval($currentDiscount)!=0.0 && $currentDiscount!="") {
if($currentSku=='762.SSUBGMTVE') {
print("1 - ".$currentDiscount."<br>");
}
$currentDiscount*= 100;
if($currentSku=='762.SSUBGMTVE') {
print("2 - ".$currentDiscount."<br>");
}
$currentPrice = str_replace(",", ".", $currentPrice);
if($currentSku=='762.SSUBGMTVE') {
print("3 - ".$currentPrice."<br>");
}
$currentPrice = ($currentPrice * 100) / (100 - $currentDiscount);
if($currentSku=='762.SSUBGMTVE') {
print("4 - ".$currentPrice."<br>");
}
}
$currentPrice = ceil($currentPrice);
if($currentSku=='762.SSUBGMTVE') {
print("5 - ".$currentPrice."<br>");
}
$currentPrice = $currentPrice * $priceMultiplier;
if($currentSku=='762.SSUBGMTVE') {
print("6 - ".$currentPrice."<br>");
}
This is the output:
1 - 0.200000
2 - 20
3 - 39.20
4 - 49
5 - 50
6 - 100
I also tried adding a var_dump() before point debug "5":
if($currentSku=='762.SSUBGMTVE') {
var_dump($currentPrice);
print("<br>** ".ceil($currentPrice)." **<br>");
}
$currentPrice = ceil($currentPrice);
if($currentSku=='762.SSUBGMTVE') {
var_dump($currentPrice);
}
if($currentSku=='762.SSUBGMTVE') {
print("5 - ".$currentPrice."<br>");
}
And this is the output:
1 - 0.200000
2 - 20
3 - 39.20
4 - 49
float(49)
** 50 **
float(50)
5 - 50
6 - 100
And just as the final stroke, just for fun:
if($currentSku=='762.SSUBGMTVE') {
print("4 - ".$currentPrice."<br>");
print("Check this out!: ceil(49) results in ".ceil(49).", but ceil(".$currentPrice.") results in ".ceil($currentPrice)." ... HAHAHA!!<br>");
}
Result:
4 - 49
Check this out!: ceil(49) results in 49, but ceil(49) results in 50 ... HAHAHA!!
Could someone explain what is happening here, please?
Maybe I'm completely blinded and there's something obvious right on my nose, but right now, it doesn't matter how many times I read the code and how many tests I do, but it doesn't make any sense.
Thanks!