This PHP function will give you output based on INR formatting, pass the parameter, and you are done.
function numberToCurrency($number){
$checkMinusVal = explode('-',$number)[0];
$checkMinus = $final = '';
$allStr = explode('.',$number);
if($checkMinusVal == ''){
$checkMinus = '-';
$allStr = explode('.',explode('-',$number)[1]);
}
$str = $allStr[0];
$length = strlen($str);
$count = $first = 0;
for($i = $length; $i >= 0; $i--){
if($count == 3 && $first == 0){
$final .= $str[$i];
if($str[$i + 1] != ''){
$final .= ',';
}
$count = 0;
$first = 1;
}
elseif($count == 2 && $first == 1){
if( ($i - 1) < 0){
$final .= $str[$i];
}
else{
$final .= $str[$i].',';
}
$count = 0;
}
else{
$final .= $str[$i];
}
$count++;
}
$final = strrev($final);
if(array_key_exists("1",$allStr)){
$decimalVal = $allStr[1][0];
if(!empty($allStr[1][1])){
$decimalVal .= $allStr[1][1];
}
else{
$decimalVal .= 0;
}
if($allStr[1][2] >= 5){
$decimalVal++;
}
$final .= '.'.$decimalVal;
}
return $checkMinus.'₹'.$final.'/-';
}
In your page call numberToCurrency($number) function and it will format and give you output in INR formatting only.
Sample Output:-
echo numberToCurrency('123') => ₹123/-
echo numberToCurrency('1234') => ₹1,234/-
echo numberToCurrency('1234567') => ₹12,34,567/-
echo numberToCurrency('1234567.123') => ₹12,34,567.12/-
echo numberToCurrency('1234567.125') => ₹12,34,567.13/-
echo numberToCurrency('-1234567.125') => -₹12,34,567.13/-