Last active
November 15, 2018 07:54
-
-
Save thatal/b533f0aad17663c1b4e387481879b5b6 to your computer and use it in GitHub Desktop.
Number to Indian Numbering system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rounding of adjusted for decimal number.