Forum Moderators: coopster
number_format($number, 0, '.', ' '); // Full prices
// Country 1 full prices
$aa11f = "1,050";
$aa12f = "2,460";
// Country 2 full prices
$bb21f = "29,99";
$bb22f = "89,99";
// Country 3 full prices
$cc31f = "669";
$cc32f = "1 699";
//Sale prices
// Country 1 sale prices
$aa11 = "950";
$aa12 = "1,460";
// Country 2 sale prices
$bb21 = "19,99";
$bb22 = "69,99";
// Country 3 sale prices
$cc31 = "569";
$cc32 = "1 299";
//Discount calculations
$aa11d = $aa11f - $aa11;
$aa12d = $aa12f - $aa12;
$bb11d = $bb11f - $bb11;
$bb12d = $bb12f - $bb12;
$cc11d = $cc11f - $cc11;
$cc12d = $cc12f - $cc12; Numbers like 5,99 or 5.99 are taken as they are
I'm having a PHP file where I store prices, and calculate discounts for various .
Therefore, with the default PHP settings, this is not treated as 1099.
Numbers like 5,99 or 5.99 are taken as they are, so my discount calculations are right.
I saw how to set the thousand separator:
// Full prices
// Country 1 full prices
$aa11f = "1,050";
$aa12f = "2,460";
// Country 2 full prices
$bb21f = "29,99";
$bb22f = "89,99";
// Country 3 full prices
$cc31f = "669";
$cc32f = "1 699";
//Sale prices
// Country 1 sale prices
$aa11 = "950";
$aa12 = "1,460";
// Country 2 sale prices
$bb21 = "19,99";
$bb22 = "69,99";
// Country 3 sale prices
$cc31 = "569";
$cc32 = "1 299";
// Discount calculations
$aa11d = $aa11f - $aa11; // -949 (WRONG - Should be 100)
$aa12d = $aa12f - $aa12; // 1 (WRONG - Should be 1000)
$bb21d = $bb21f - $bb21; // 10 (CORRECT - However, this is a fluke!)
$bb22d = $bb22f - $bb22; // 20 (CORRECT - Again, a fluke!)
$cc31d = $cc31f - $cc31; // 100 (CORRECT)
$cc32d = $cc32f - $cc32; // 0 (WRONG - Should be 400)
/**
* Convert formatted price string to a number.
* Assumptions/rules:
* - Comma can be the decimal point(dp) character OR thousands separator
* - Dot can be a thousands separator when a comma is used as the dp character (or there is more than 1 dot)
* - When a dp char is used, assume there is always 2 decimal places
* - Space can be a thousands separator only (simply remove spaces unconditionally)
* @param string $priceStr A formatted price string of the form "1,050", "29,99", "1 299" or "1.234.567,99" etc.
* @return float The converted price as a float
*/
function priceStringToNumber($priceStr) {
$number = $priceStr;
// Check for comma as dp OR dot as thousands separator (and no dp)
if (preg_match('/,\d\d$/',$number) || preg_match('/\.\d\d\d$/',$number)) {
// 1. Remove all dots (since only used as thousands separator)
// 2. Replace comma with dot (decimal point) if applicable
$number = str_replace(['.',','], ['','.'], $number);
}
// 3. Remove all commas (since any remaining commas must be thousand separators or erroneous)
// 4. Remove all spaces (possible thousands separators or erroneous)
$number = str_replace([',',' '], '', $number);
return (float)$number;
}
/**
* Calculate discount
* - Acts as a wrapper to priceStringToNumber()
* @param string $fullPriceStr Formatted full price string
* @param string $salePriceStr Formatted sale price string
* @return float The numeric value of $fullPriceStr less $salePriceStr
*/
function calcDiscount($fullPriceStr,$salePriceStr) {
$fullPrice = priceStringToNumber($fullPriceStr);
$salePrice = priceStringToNumber($salePriceStr);
$discount = $fullPrice - $salePrice;
return $discount;
}
// Discount calculations
$aa11d = calcDiscount($aa11f,$aa11); // 100 (CORRECT)
$aa12d = calcDiscount($aa12f,$aa12); // 1000 (CORRECT)
$bb21d = calcDiscount($bb21f,$bb21); // 10 (CORRECT)
$bb22d = calcDiscount($bb22f,$bb22); // 20 (CORRECT)
$cc31d = calcDiscount($cc31f,$cc31); // 100 (CORRECT)
$cc32d = calcDiscount($cc32f,$cc32); // 400 (CORRECT)
// Test conversions
echo priceStringToNumber('1,050');// 1050
echo priceStringToNumber('29,99');// 29.99
echo priceStringToNumber('669');// 669
echo priceStringToNumber('1 669');// 1699
echo priceStringToNumber('1.669');// 1699 (Dot is thousand separator)
echo priceStringToNumber('1,234,567.99');// 1234567.99
echo priceStringToNumber('1.234.567,99');// 1234567.99
echo priceStringToNumber('1 234 567,99');// 1234567.99
echo priceStringToNumber('1 234 567.99');// 1234567.99
echo priceStringToNumber('1 234,567.99');// 1234567.99
echo priceStringToNumber('1 234.567,99');// 1234567.99
echo priceStringToNumber('1 234 567');// 1234567
echo priceStringToNumber('1,234,567');// 1234567
echo priceStringToNumber('1.234.567');// 1234567