Googled for Short Number Format in PHP, got thousands of working results from StackOverflow, Github, etc.. but I didn't get any results for Indian Format.
Eg:
In other countries: 1000 is as 1k, 100000 is as 100k, 10000000 is as 10m
But in India: 1000 is as 1k OR 1T, 100000 is as 1L, 10000000 is as 1c
Can anyone help me to do that?
With the help of this answer
I came up with a solution for you:
<?php
function indian_short_number($n) {
if($n <= 99999){
$precision = 3;
if ($n < 1000) {
// Anything less than a thousand
$n_format = number_format($n);
} else {
// At least a thousand
$n_format = number_format($n / 1000, $precision) . ' K';
}
/* Use this code if you want to round off your results
$n_format = ceil($n_format / 10) * 10;
if($n >= 1000){
$n_format = $n_format . ' K';
}
*/
return $n_format;
}
else{
$precision = 2;
if ($n > 99999 && $n < 9999999) {
// Anything more than a Lac and less than crore
$n_format = number_format($n / 100000, $precision) . ' L';
} elseif ($n > 999999) {
// At least a crore
$n_format = number_format($n / 10000000, $precision) . ' C';
}
/* Use this code if you want to round off your results
$n_format = ceil($n_format / 10) * 10;
if($n >= 10000 && $n < 10000000){
$n_format = $n_format . ' L';
}
elseif($n >= 1000000){
$n_format = $n_format . ' C';
}
*/
return $n_format;
}
}
echo indian_short_number(10000);
?>
The code for rounding off is not proper. (For 18100
it rounds to 20 K
instead of 19 K
)
I will be thankful if any of the visitors edit the answer with the fixing it.
Hope it helps you.