I want to replace the currency shorten alphabet, it is currently in M, L and So on. I want to change to indian currency number format like K, L and CR
below is code:
if(!function_exists('houzez_number_shorten')) {
function houzez_number_shorten($number, $precision = 0, $divisors = null) {
$number = houzez_clean_price_20($number);
if (!isset($divisors)) {
$divisors = array(
pow(1000, 0) => '', // 1000^0 == 1
pow(1000, 1) => 'K', // Thousand
pow(1000, 2) => 'M', // Million
pow(1000, 3) => 'B', // Billion
pow(1000, 4) => 'T', // Trillion
pow(1000, 5) => 'Qa', // Quadrillion
pow(1000, 6) => 'Qi', // Quintillion
);
}
foreach ($divisors as $divisor => $shorthand) {
if (abs($number) < ($divisor * 1000)) {
// Match found
break;
}
}
//Match found or not found use the last defined value for divisor
$price = number_format($number / $divisor, 1);
$price = str_replace(".0","",$price);
return $price . $shorthand;
}
}
I am not good at coding, so any help will be appreciated
I want to replace the currency shorten alphabet, it is currently in M, L and So on. I want to change to indian currency number format like K, L and CR
below is code:
if(!function_exists('houzez_number_shorten')) {
function houzez_number_shorten($number, $precision = 0, $divisors = null) {
$number = houzez_clean_price_20($number);
if (!isset($divisors)) {
$divisors = array(
pow(1000, 0) => '', // 1000^0 == 1
pow(1000, 1) => 'K', // Thousand
pow(1000, 2) => 'M', // Million
pow(1000, 3) => 'B', // Billion
pow(1000, 4) => 'T', // Trillion
pow(1000, 5) => 'Qa', // Quadrillion
pow(1000, 6) => 'Qi', // Quintillion
);
}
foreach ($divisors as $divisor => $shorthand) {
if (abs($number) < ($divisor * 1000)) {
// Match found
break;
}
}
//Match found or not found use the last defined value for divisor
$price = number_format($number / $divisor, 1);
$price = str_replace(".0","",$price);
return $price . $shorthand;
}
}
I am not good at coding, so any help will be appreciated
Share Improve this question edited Dec 26, 2020 at 5:30 Q Studio 2,5267 gold badges25 silver badges39 bronze badges asked Dec 25, 2020 at 15:16 Vijay PatilVijay Patil 53 bronze badges1 Answer
Reset to default 1The divisors loop assumes that the next row is 1000 times larger than the current one so I think you'll have to change the logic here, e.g. keep iterating until the divisor is too big and use the previous one e.g.
if (!isset($divisors)) {
$divisors = array(
pow(10, 0) => '',
pow(10, 3) => 'K', // Thousand
pow(10, 5) => 'L', // Lakh
pow(10, 7) => 'CR', // Crore
);
}
$divisor = 1;
$shorthand = '';
foreach ($divisors as $next_divisor => $next_shorthand) {
if (abs($number) < $next_divisor) {
// Too big; use previous row
break;
}
$divisor = $next_divisor;
$shorthand = $next_shorthand;
}
I'd guess that's all you need to change. (I don't know if you need to tell number_format to not use thousands too but hopefully it'll take that from the locale, and you wouldn't be showing thousands unless you've got 1000+ crore anyway.) Note also that the existing function does not use the $precision parameter.