How to convert amount in crores to decimal value in php?
0 5532
Answer: To convert amount in crores to decimal value in php, here's the code:
<?php
// Create a function for converting the amount to decimal
function convertCrtoDecimal($amount) {
$Arraycheck = array("4"=>"K","5"=>"K","6"=>"Lacs","7"=>"Lacs","8"=>"Cr","9"=>"Cr");
// define decimal values
$numberLength = strlen($amount); //count the length of numbers
if ($numberLength > 3) {
foreach ($Arraycheck as $Lengthnum=>$unitval) {
if ($numberLength == $Lengthnum) {
if ($Lengthnum % 2 == 0) {
$RanNumber = substr($amount,1,2);
$NmckGtZer = ($RanNumber[0]+$RanNumber[1]);
if ($NmckGtZer < 1) {
$RanNumber = "0";
} else {
if ($RanNumber[1] == 0) {
$RanNumber[1] = "0";
}
}
$amount = substr($amount,0,$numberLength-$Lengthnum+1) . "." . $RanNumber . " $unitval ";
} else {
$RanNumber = substr($amount,2,2);
$NmckGtZer = ($RanNumber[0]+$RanNumber[1]);
if ($NmckGtZer < 1) {
$RanNumber = 0;
} else {
if ($RanNumber[1] == 0) {
$RanNumber[1] = "0";
}
}
$amount = substr($amount,0,$numberLength-$Lengthnum+2) . ".". $RanNumber . " $unitval";
}
}
}
} else {
$amount . "Rs";
}
return $amount;
}
?>
<!-- Call the function here -->
<?php $amount=455000; //nummeric value in variable
$get_amount= convertCrtoDecimal($amount);
echo 'Rs '.$get_amount;
?>
Output: Rs 4.55 Lacs
Share:
Comments
Waiting for your comments