php°¢À­²®Êý×ÖתÖÐÎÄÈËÃñ±Ò´óд

5年以前  |  阅读数:746 次  |  编程语言:PHP 

±¾ÎÄʵÀýΪ´o¼Ò*ÖÏiÁËphp°¢À­²®Êý×ÖתÖÐÎÄÈËÃñ±Ò´oдµÄʵÏÖ´uÂe£¬¹(C)´o¼Ò²Î¿¼£¬¾ßÌa´uÂeÈçÏÂ

´uÂe1£ºphp°¢À­²®Êý×ÖתÖÐÎÄÈËÃñ±Ò´oд£¬ÓÐÏeϸµÄ×¢ÊÍ


    /**
    *Êý×Ö½ð¶iת»»³ÉÖÐÎÄ´oд½ð¶iµÄº¯Êý
    *String Int $num Ҫת»»µÄСдÊý×Ö»oСд×Ö*u´®
    *return ´oд×Öĸ
    *СÊýλΪÁ½Î»
    **/
    function num_to_rmb($num){
        $c1 = "ÁaÒ¼*¡ÈþËÁÎe½Æa°Æ¾Á";
        $c2 = "*Ö½ÇԪʰ°ÛǪÍoÊ°°ÛǪÒÚ";
        //¾«È*µ½*ÖºoÃae¾Í²»ÒªÁË£¬ËuÒÔÖ»ÁoÁ½¸oСÊýλ
        $num = round($num, 2); 
        //½«Êý×Öת»¯ÎªÕuÊý
        $num = $num * 100;
        if (strlen($num) > 10) {
            return "½ð¶iÌ«´o£¬Çe¼i²e";
        } 
        $i = 0;
        $c = "";
        while (1) {
            if ($i == 0) {
                //»ñÈ¡×iºoһλÊý×Ö
                $n = substr($num, strlen($num)-1, 1);
            } else {
                $n = $num % 10;
            }
            //ÿ´Î½«×iºoһλÊý×Öת»¯ÎªÖÐÎÄ
            $p1 = substr($c1, 3 * $n, 3);
            $p2 = substr($c2, 3 * $i, 3);
            if ($n != '0' || ($n == '0' && ($p2 == 'ÒÚ' || $p2 == 'Ío' || $p2 == 'Ôª'))) {
                $c = $p1 . $p2 . $c;
            } else {
                $c = $p1 . $c;
            }
            $i = $i + 1;
            //È¥µoÊý×Ö×iºoһλÁË
            $num = $num / 10;
            $num = (int)$num;
            //½aÊøÑ­»*
            if ($num == 0) {
                break;
            } 
        }
        $j = 0;
        $slen = strlen($c);
        while ($j < $slen) {
            //utf8Ò»¸oºº×ÖÏaµ±3¸o×Ö*u
            $m = substr($c, $j, 6);
            //´¦ÀiÊý×ÖÖкܶa0µÄÇe¿o,ÿ´ÎÑ­»*È¥µoÒ»¸oºº×Ö¡°Áa¡±
            if ($m == 'ÁaÔª' || $m == 'ÁaÍo' || $m == 'ÁaÒÚ' || $m == 'ÁaÁa') {
                $left = substr($c, 0, $j);
                $right = substr($c, $j + 3);
                $c = $left . $right;
                $j = $j-3;
                $slen = $slen-3;
            } 
            $j = $j + 3;
        } 
        //Õa¸oÊÇΪÁËÈ¥µoÀaËÆ23.0ÖÐ×iºoÒ»¸o¡°Áa¡±×Ö
        if (substr($c, strlen($c)-3, 3) == 'Áa') {
            $c = substr($c, 0, strlen($c)-3);
        }
        //½«´¦ÀiµÄºº×Ö¼ÓÉÏ¡°Õu¡±
        if (empty($c)) {
            return "ÁaÔªÕu";
        }else{
            return $c . "Õu";
        }
    }
    echo num_to_rmb(23000000.00); //*¡ÇªÈþ°ÛÍoÔªÕu

´uÂe2£ºphp°¢À­²®Êý×ÖתÖÐÎÄ´oд½ð¶i


    // °¢À­²®Êý×ÖתÖÐÎÄ´oд½ð¶i
    function NumToCNMoney($num,$mode = true,$sim = true){
      if(!is_numeric($num)) return 'º¬ÓÐ*ÇÊý×Ö*ÇСÊýµa×Ö*u£¡';
      $char  = $sim ? array('Áa','Ò»','¶þ','Èý','ËÄ','Îa','Áu','Æß','°Ë','¾Å')
      : array('Áa','Ò¼','*¡','Èþ','ËÁ','Îe','½','Æa','°Æ','¾Á');
      $unit  = $sim ? array('','Ê®','°Ù','ǧ','','Ío','ÒÚ','Õ×')
      : array('','Ê°','°Û','Ǫ','','Èf','ƒ|','Õ×');
      $retval = $mode ? 'Ôª':'µa';
      //СÊý²¿*Ö
      if(strpos($num, '.')){
        list($num,$dec) = explode('.', $num);
        $dec = strval(round($dec,2));
        if($mode){
          $retval .= "{$char[$dec['0']]}½Ç{$char[$dec['1']]}*Ö";
        }else{
          for($i = 0,$c = strlen($dec);$i < $c;$i++) {
            $retval .= $char[$dec[$i]];
          }
        }
      }
      //ÕuÊý²¿*Ö
      $str = $mode ? strrev(intval($num)) : strrev($num);
      for($i = 0,$c = strlen($str);$i < $c;$i++) {
        $out[$i] = $char[$str[$i]];
        if($mode){
          $out[$i] .= $str[$i] != '0'? $unit[$i%4] : '';
            if($i>1 and $str[$i]+$str[$i-1] == 0){
            $out[$i] = '';
          }
            if($i%4 == 0){
            $out[$i] .= $unit[4+floor($i/4)];
          }
        }
      }
      $retval = join('',array_reverse($out)) . $retval;
      return $retval;
    }


    echo (NumToCNMoney(2.55)."<br>");
    echo (NumToCNMoney(2.55,1,0)."<br>");
    echo (NumToCNMoney(7965)."<br>");
    echo (NumToCNMoney(7965,1,0)."<br>");
    echo (NumToCNMoney(155555555.68)."<br>");
    echo (NumToCNMoney(155555555.68,1,0)."<br>");
    echo (NumToCNMoney(0.8888888)."<br>");
    echo (NumToCNMoney(0.8888888,1,0)."<br>");
    echo (NumToCNMoney(99999999999)."<br>");
    echo (NumToCNMoney(99999999999,1,0)."<br>");

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Òѧϰphp³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8