search
HomeBackend DevelopmentPHP TutorialConvert Arabic numerals to Chinese RMB capital letters, _PHP tutorial

php converts Arabic numerals to Chinese RMB capital letters,

The example in this article shares with you the implementation code of converting php Arabic numerals to Chinese RMB capital letters for your reference. The specific code is as follows

Code 1: Convert php Arabic numerals to Chinese RMB capital letters, with detailed comments

/**
*数字金额转换成中文大写金额的函数
*String Int $num 要转换的小写数字或小写字符串
*return 大写字母
*小数位为两位
**/
function num_to_rmb($num){
    $c1 = "零壹贰叁肆伍陆柒捌玖";
    $c2 = "分角元拾佰仟万拾佰仟亿";
    //精确到分后面就不要了,所以只留两个小数位
    $num = round($num, 2); 
    //将数字转化为整数
    $num = $num * 100;
    if (strlen($num) > 10) {
        return "金额太大,请检查";
    } 
    $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) {
        //utf8一个汉字相当3个字符
        $m = substr($c, $j, 6);
        //处理数字中很多0的情况,每次循环去掉一个汉字“零”
        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;
    } 
    //这个是为了去掉类似23.0中最后一个“零”字
    if (substr($c, strlen($c)-3, 3) == '零') {
        $c = substr($c, 0, strlen($c)-3);
    }
    //将处理的汉字加上“整”
    if (empty($c)) {
        return "零元整";
    }else{
        return $c . "整";
    }
}
echo num_to_rmb(23000000.00); //贰仟叁佰万元整

Code 2: Convert php Arabic numerals to Chinese uppercase amounts

// 阿拉伯数字转中文大写金额
function NumToCNMoney($num,$mode = true,$sim = true){
  if(!is_numeric($num)) return '含有非数字非小数点字符!';
  $char  = $sim &#63; array('零','一','二','三','四','五','六','七','八','九')
  : array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
  $unit  = $sim &#63; array('','十','百','千','','万','亿','兆')
  : array('','拾','佰','仟','','萬','億','兆');
  $retval = $mode &#63; '元':'点';
  //小数部分
  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]];
      }
    }
  }
  //整数部分
  $str = $mode &#63; 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'&#63; $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>");

I hope this article will help you learn PHP programming.

Articles you may be interested in:

  • Implementing a function to convert Arabic numerals to Chinese numerals under php
  • php separates strings into string arrays by uppercase letters
  • Use PHP to convert lowercase amounts to uppercase amounts (accurate to minutes)
  • PHP function code to convert RMB amount numbers to Chinese uppercase
  • php converts all strings into uppercase or Lowercase method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084584.htmlTechArticlephp converts Arabic numerals to Chinese RMB capital letters. This example shares the implementation code for converting php Arabic numerals to Chinese RMB capital letters. , for your reference, the specific code is as follows Code 1:...
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.