如何在PHP中将crores中的金额转换为十进制值?

时间:2015-10-13 06:25:18

标签: php number-formatting

我的产品价格为5005卢比/(50万卢比和5千卢比)。我想转换成50.5万卢比。是否有任何PHP功能来做到这一点。我尝试搜索stackoverflow并继续只获得number_words转换。在一个名为99英亩的网站中,他们这样做:http://www.99acres.com/Bangalore-Real-Estate.htm。任何人都可以帮忙

6 个答案:

答案 0 :(得分:8)

    <?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. 计算number_of_digit
  2. 取(number_of_digit-1)数十位数值
  3. 分开。
  4. 从位数中找到值的扩展名。
  5. 输出
  6.   

    123456789 = 12.34 Cr

         

    23456789 = 2.34 Cr

         

    3456789 = 34.56 Lac

         

    456789 = 4.56 Lac

         

    56789 = 56.78 K

         

    6789 = 6.78 K

    如果任何测试用例失败。请告诉我。我会纠正

答案 1 :(得分:2)

你也可以尝试我的代码,可能它会帮助你:

      function no_to_words($no)
{
    if($no == 0) {
        return ' ';

    }else {
        $n =  strlen($no); // 7
        switch ($n) {
            case 3:
                $val = $no/100;
                $val = round($val, 2);
                $finalval =  $val ." hundred";
                break;
            case 4:
                $val = $no/1000;
                $val = round($val, 2);
                $finalval =  $val ." thousand";
                break;
            case 5:
                $val = $no/1000;
                $val = round($val, 2);
                $finalval =  $val ." thousand";
                break;
            case 6:
                $val = $no/100000;
                $val = round($val, 2);
                $finalval =  $val ." lakh";
                break;
            case 7:
                $val = $no/100000;
                $val = round($val, 2);
                $finalval =  $val ." lakh";
                break;
            case 8:
                $val = $no/10000000;
                $val = round($val, 2);
                $finalval =  $val ." crore";
                break;
            case 9:
                $val = $no/10000000;
                $val = round($val, 2);
                $finalval =  $val ." crore";
                break;

            default:
                echo "";
        }
        return $finalval;

    }
}

echo no_to_words(12345676);

您可以根据自己的要求修改功能。

答案 2 :(得分:1)

只需找到输入数字的长度,根据其长度(crores或lakhs)进行划分,将数字四舍五入为2位数,然后使用数字添加扩展名。

function convertCurrency($number)
{
    // Convert Price to Crores or Lakhs or Thousands
    $length = strlen($number);
    $currency = '';

    if($length == 4 || $length == 5)
    {
        // Thousand
        $number = $number / 1000;
        $number = round($number,2);
        $ext = "Thousand";
        $currency = $number." ".$ext;
    }
    elseif($length == 6 || $length == 7)
    {
        // Lakhs
        $number = $number / 100000;
        $number = round($number,2);
        $ext = "Lac";
        $currency = $number." ".$ext;

    }
    elseif($length == 8 || $length == 9)
    {
        // Crores
        $number = $number / 10000000;
        $number = round($number,2);
        $ext = "Cr";
        $currency = $number.' '.$ext;
    }

    return $currency;
}

// Call the function with a number input 
convertCurrency(100000);

干净简单。 :) (如果任何测试用例失败,请告诉我。我会修复它)

答案 3 :(得分:0)

试试这可能会对你有帮助,

 function cnvCrtoDec($number) {
     $unitArray =   array("4"=>"K","5"=>"K","6"=>"L","7"=>"L","8"=>"Cr","9"=>"Cr");
     $numLen        = strlen($number);
     if ($numLen > 3 ) {
        foreach ($unitArray as $nmLen=>$unit) {
            if ($numLen == $nmLen) {
               if ($nmLen % 2 == 0) {
                  $remNumber    = substr($number,1,3);
                  $ckNmGtZer    = ($remNumber[0]+$remNumber[1]+$remNumber[2]);
                  if ($ckNmGtZer < 1) { 
                      $remNumber    = "";
                  } else {
                     if ( ($remNumber[1]    == 0) && ($remNumber[2] == 0)   ) {
                        $remNumber[1]   = "";
                        $remNumber[2]   = "";   
                  } 
             }
             echo $number   = substr($number,0,$numLen-$nmLen+1) . "." .  $remNumber . "$unit";             
    } else {
         $remNumber     = substr($number,2,3);
         $ckNmGtZer = ($remNumber[0]+$remNumber[1]+$remNumber[2]);
         if ($ckNmGtZer < 1) { 
            $remNumber  = 0;
        } else {
            if (($remNumber[1]  == 0) && ($remNumber[2] == 0)) {
                $remNumber[1]   = "";
                $remNumber[2]   = "";   
            }   
        }
        echo $number    = substr($number,0,$numLen-$nmLen+2) . ".". $remNumber . "$unit";   
       }
     }
 }
 } else {
    echo $number . "Rs";    
 }
 }

 cnvCrtoDec($number     = "155005");

答案 4 :(得分:0)

以下是Swift方法,在PHP中将其转换后可能会发现很有用 并且可能是其他人发现它对完全相同的答案使用完全查找,并且如果您只想使用没有货币符号的格式编号,那么下面的代码可能会有用

SWIFT 4

static func convertToCurrency(value: Double) -> String {
    var shortAmt = value
    var suf = ""

    if (value >= 10000000.0) {
        suf = " C"
        shortAmt /= 10000000.0
    } else if (value >= 1000000.0) {
        suf = " M";
        shortAmt /= 1000000.0;
    } else if (value >= 100000.0) {
        suf = " L"
        shortAmt /= 100000.0
    } else if (value >= 1000.0) {
        suf = " K"
        shortAmt /= 1000.0
    }

    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .currency
    numberFormatter.maximumFractionDigits = 2
    numberFormatter.locale = Locale(identifier: "en-IN") // Define your currency symbol type
    //numberFormatter.currencySymbol = ""   //Add this line if you don't want to add currency symbol
    let amtAsStr = numberFormatter.string(from: shortAmt as NSNumber)

    return "\(amtAsStr ?? "")\(suf)"
}

答案 5 :(得分:0)

function convert_rupee($amount){
    $length = strlen($amount);
    if($length>=6 && $length <=7){
        echo round($amount/100000,2).' Lac(s)';
    }else if($length>=8 && $length <=9){
        echo round($amount/10000000,2).' Cr.';
    }else if($length>=4 && $length <=5){
        echo round($amount/1000,2).' K';

    }else{ echo 0;}
}