• 数字转换人民币


    数字转换人民币

    /**
     * 数字转人名币
     * @param [type] $num [description]
     * @return [type]  [description]
     */
    function numtormb ($num) {
     $c1 = "零壹贰叁肆伍陆柒捌玖";
     $c2 = "分角元拾佰仟万拾佰仟亿";
     $num = round($num, 2);
     $num = $num * 100;
     if (strlen($num) > 10) {
      return "oh,sorry,the number is too long!";
     }
     $i = 0;
     $c = "";
     while (1) {
      if ($i == 0) {
       $n = substr($num, strlen($num)-1, 1);
      } else {
       $n = $num % 10;
      }
      $p1 = substr($c1, 3 * $n, 3);
      $p2 = substr($c2, 3 * $i, 3);
      if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
       $c = $p1 . $p2 . $c;
      } else {
       $c = $p1 . $c;
      }
      $i = $i + 1;
      $num = $num / 10;
      $num = (int)$num;
      if ($num == 0) {
       break;
      }
     }
     $j = 0;
     $slen = strlen($c);
     while ($j < $slen) {
      $m = substr($c, $j, 6);
      if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
       $left = substr($c, 0, $j);
       $right = substr($c, $j + 3);
       $c = $left . $right;
       $j = $j-3;
       $slen = $slen-3;
      }
      $j = $j + 3;
     }
     if (substr($c, strlen($c)-3, 3) == '零') {
      $c = substr($c, 0, strlen($c)-3);
     } // if there is a '0' on the end , chop it out
     return $c . "整";
    }
    
    $num=200.12;
    echo numtormb ($num);
    

      人民币金额转大写

    // 人民币金额转大写程序
    class Ext_Num2Cny{
      static $basical = array(0=>"零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
      static $advanced=array(1=>"拾","佰","仟");
      
      public static function ParseNumber($number){
        $number=trim($number);
        if ($number>999999999999) return "数字太大,无法处理。抱歉!";
        if ($number==0) return "零";
        if(strpos($number,'.')){
          $number=round($number,2);
          $data=explode(".",$number);
          $data[0]=self::int($data[0]);
          $data[1]=self::dec($data[1]);
          return $data[0].$data[1];
        }else{
          return self::int($number).'整';
        }
      }
      
      public static function int($number){
        $arr=array_reverse(str_split($number));
        $data='';
        $zero=false;
        $zero_num=0;
        foreach ($arr as $k=>$v){
          $_chinese='';
          $zero=($v==0)?true:false;
          $x=$k%4;
          if($x && $zero && $zero_num>1)continue;
          switch ($x){
            case 0:
              if($zero){
                $zero_num=0;
              }else{
                $_chinese=self::$basical[$v];
                $zero_num=1;
              }
              if($k==8){
                $_chinese.='亿';
              }elseif($k==4){
                $_chinese.='万';
              }
              break;  
            default:
              if($zero){
                if($zero_num==1){
                  $_chinese=self::$basical[$v];
                  $zero_num++;
                }
              }else{
                $_chinese=self::$basical[$v];
                $_chinese.=self::$advanced[$x];
              }
          }
          $data=$_chinese.$data;
        }
        return $data.'元';
      }
         
      public static function dec($number){
        if(strlen($number)<2) $number.='0';
        $arr=array_reverse(str_split($number));
        $data='';
        $zero_num=false;
        foreach ($arr as $k=>$v){
          $zero=($v==0)?true:false;
          $_chinese='';
          if($k==0){
            if(!$zero){
              $_chinese=self::$basical[$v];
              $_chinese.='分';
              $zero_num=true;
            }
          }else{
            if($zero){
              if($zero_num){
                $_chinese=self::$basical[$v];
              }
            }else{
              $_chinese=self::$basical[$v];
              $_chinese.='角';
            }
          }
          $data=$_chinese.$data;
        }
        return $data;
      }
    }   
         
      echo  Ext_Num2Cny::ParseNumber(238956.23);
  • 相关阅读:
    C++调用外部应用程序
    SVN文件加锁
    vs ComboBox显示多行
    __slots__ Python Class限制添加属性
    Python数据分析之pandas学习
    整理Lua和Unity和Lua交互文章链接
    [整理]Unity3D游戏开发之Lua
    ping telnet ssh netstat
    java rpc
    css 手机适配
  • 原文地址:https://www.cnblogs.com/objects/p/7122259.html
Copyright © 2020-2023  润新知