Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert amount in crores to decimal value in php?

I have a price of the product which is Rs 5005000/- (50 lakh and 5 thousand rupees). I want to convert to like 50.5 lakhs. Is there any PHP functions to do that. I tried searching StackOverflow and keep getting number_words conversion only. In a site called 99 acres they have done it like this: http://www.99acres.com/Bangalore-Real-Estate.htm. Can anyone help

like image 905
rahul Avatar asked Oct 13 '15 06:10

rahul


1 Answers

    <?php
function count_digit($number) {
  return strlen($number);
}

function divider($number_of_digits) {
    $tens="1";

  if($number_of_digits>8)
    return 10000000;

  while(($number_of_digits-1)>0)
  {
    $tens.="0";
    $number_of_digits--;
  }
  return $tens;
}
//function call
$num = "789";
$ext="";//thousand,lac, crore
$number_of_digits = count_digit($num); //this is call :)
    if($number_of_digits>3)
{
    if($number_of_digits%2!=0)
        $divider=divider($number_of_digits-1);
    else
        $divider=divider($number_of_digits);
}
else
    $divider=1;

$fraction=$num/$divider;
$fraction=number_format($fraction,2);
if($number_of_digits==4 ||$number_of_digits==5)
    $ext="k";
if($number_of_digits==6 ||$number_of_digits==7)
    $ext="Lac";
if($number_of_digits==8 ||$number_of_digits==9)
    $ext="Cr";
echo $fraction." ".$ext;
?>
  1. count the number_of_digit
  2. Take (number_of_digit-1) digit tens value
  3. divide it.
  4. find the extension of value from number of digits.
  5. output will be

123456789 = 12.34 Cr

23456789 = 2.34 Cr

3456789 = 34.56 Lac

456789 = 4.56 Lac

56789 = 56.78 K

6789 = 6.78 K

If any testcase fails . Please let me know. I will rectify

like image 132
Rohan Khude Avatar answered Sep 22 '22 10:09

Rohan Khude