Home php教程 php手册 一个阿拉伯数字转中文数字的函数

一个阿拉伯数字转中文数字的函数

Jun 13, 2016 pm 12:44 PM
indivual Chinese function number most of change need

最近因需要,写了个“阿拉伯数字转中文数字的函数”。搜索了精华区只见到一个类似的。
感觉到我的算法不错,所以贴出来共享一下如果要用于金额的转换,对小数部分的处理要做一下修改
function ch_num($num,$mode=true) {
  $char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
  $dw = array("","拾","佰","仟","","萬","億","兆");
  $dec = "點";
  $retval = "";

  if($mode)
    preg_match_all("/^0*(\d*)\.?(\d*)/",$num, $ar);
  else
    preg_match_all("/(\d*)\.?(\d*)/",$num, $ar);

  if($ar[2][0] != "")
    $retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数
  if($ar[1][0] != "") {
    $str = strrev($ar[1][0]);
    for($i=0;$i      $out[$i] = $char[$str[$i]];
      if($mode) {
        $out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
        if($str[$i]+$str[$i-1] == 0)
          $out[$i] = "";
        if($i%4 == 0)
          $out[$i] .= $dw[4+floor($i/4)];
      }
    }
    $retval = join("",array_reverse($out)) . $retval;
  }
  return $retval;
}

//echo ch_num("12345006789001.123");
//echo ch_num("880079.1234");
echo ch_num("300045.0123");

?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

Considerations for parameter order in C++ function naming

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

How to write efficient and maintainable functions in Java?

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

Complete collection of excel function formulas

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

What is the difference between custom PHP functions and predefined functions?

Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed Aug 10, 2024 pm 10:15 PM

Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

C++ Function Exception Advanced: Customized Error Handling

Special usage of C++ function default parameters and variable parameters in template programming Special usage of C++ function default parameters and variable parameters in template programming Apr 22, 2024 pm 03:12 PM

Special usage of C++ function default parameters and variable parameters in template programming

See all articles