Skip to content

Instantly share code, notes, and snippets.

@thatal
Last active November 15, 2018 07:54
Show Gist options
  • Save thatal/b533f0aad17663c1b4e387481879b5b6 to your computer and use it in GitHub Desktop.
Save thatal/b533f0aad17663c1b4e387481879b5b6 to your computer and use it in GitHub Desktop.
Number to Indian Numbering system.
function IndianMoneyFormat($number, $decimal = 2) {
$explrestunits = "" ;
$decimal_text = "" ;
$number = explode('.', $number);
$num = $number[0];
if(strlen($num)>3){
$lastthree = substr($num, strlen($num)-3, strlen($num));
$restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
$restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
$expunit = str_split($restunits, 2);
for($i=0; $i<sizeof($expunit); $i++){
// creates each of the 2's group and adds a comma to the end
if($i==0) {
$explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
}else{
$explrestunits .= $expunit[$i].",";
}
}
$thecash = $explrestunits.$lastthree;
} else {
$thecash = $num;
}
if($num == ""){
$thecash = 0;
}
for($i=0; $i<$decimal; $i++){
$decimal_text .= "0";
}
if(!empty($number[1])) {
if(strlen($number[1]) >= $decimal){
$new_text = round("0.".$number[1], $decimal);
$explode_text = explode(".", $new_text);
if(!isset($explode_text[1])){
$decimal_text = substr($decimal_text, 0, $decimal);
}else{
$decimal_text = (strlen($explode_text[1]) >1 ? $explode_text[1] : $explode_text[1]."0");
}
}elseif (strlen($number[1]) < $decimal) {
$decimal_text = $number[1].substr($decimal_text, 0, ($decimal_text-strlen($number[1])));
}
return $thecash.'.'.$decimal_text;
} else {
return $thecash.'.'.$decimal_text;
}
}
@thatal
Copy link
Author

thatal commented Nov 15, 2018

rounding of adjusted for decimal number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment