search
HomeBackend DevelopmentPHP ProblemHow to convert numbers to uppercase in php

php Number uppercase conversion method: 1. Determine whether $num exists; 2. Convert floating point to integer; 3. Use empty variables to save conversion strings; 4. Split numbers and distinguish circles Angular minutes; 5. Calculate billions of dollars in a loop, etc.; 6. Just output the conversion result.

How to convert numbers to uppercase in php

An amount conversion function used in CRM in the early years, I took it out from the old project and recorded it today. There are many functional methods for converting amounts, and they are all very good. However, this is a conversion function written by the author when he first started working, so it is somewhat commemorative. If you have any questions, please point them out!

Without further ado, let’s get straight to the code:

<?php

/**
* 数字金额转换大写数字
* $num 数字类型
*/

function inttodaxie($num) {
    //判断$num是否数字
    if(!is_numeric($num)) return -1;
    $dint = array(&#39;零&#39;, &#39;壹&#39;, &#39;贰&#39;, &#39;叁&#39;, &#39;肆&#39;, &#39;伍&#39;, &#39;陆&#39;, &#39;柒&#39;, &#39;捌&#39;, &#39;玖&#39;);
    $len = strlen($num);
    $dstr = &#39;&#39;;
    for($i = 0; $i <= $len; $i++) {
        $key_ = substr($num, $i, 1);
        $dstr .= $dint[$key_];
    }
    return $dstr;
}

$result = inttodaxie(90011234);
var_dump($result);
echo &#39;<br>&#39;;

The above is the basic conversion code. On this basis, a secondary transformation is carried out:

<?php

/**
* 转换大写金额
* $num 数字类型
*/

function inttod($num) {
    //判断$num是否存在
    if(!$num) return &#39;零圆&#39;;
    //保留小数点后两位
    $num = round($num, 2);
    //将浮点转换为整数
    $tem_num = $num * 100;
    //判断数字长度
    $tem_num_len = strlen($tem_num);
    if($tem_num_len > 14) {
        return &#39;数字太大了吧,有这么大的金钱吗&#39;;
    }

    //大写数字
    $dint = array(&#39;零&#39;, &#39;壹&#39;, &#39;贰&#39;, &#39;叁&#39;, &#39;肆&#39;, &#39;伍&#39;, &#39;陆&#39;, &#39;柒&#39;, &#39;捌&#39;, &#39;玖&#39;);
    //大写金额单位
    $danwei = array(&#39;仟&#39;, &#39;佰&#39;, &#39;拾&#39;, &#39;亿&#39;, &#39;仟&#39;, &#39;佰&#39;, &#39;拾&#39;, &#39;万&#39;, &#39;仟&#39;, &#39;佰&#39;, &#39;拾&#39;, &#39;圆&#39;);
    $danwei1 = array(&#39;角&#39;, &#39;分&#39;);

    //空的变量用来保存转换字符串
    $daxie = &#39;&#39;;

    //分割数字,区分圆角分
    list($left_num, $right_num) = explode(&#39;.&#39;, $num);

    //计算单位长度
    $danwei_len = count($danwei);
    //计算分割后的字符串长度
    $left_num_len = strlen($left_num);
    $right_num_len = strlen($right_num);

    //循环计算亿万元等
    for($i = 0; $i < $left_num_len; $i++) {
        //循环单个文字
        $key_ = substr($left_num, $i, 1);

        //判断数字不等于0或数字等于0与金额单位为亿、万、圆,就返回完整单位的字符串
        if($key_ !== &#39;0&#39; || ($key_ == &#39;0&#39; && ($danwei[$danwei_len - $left_num_len + $i] == &#39;亿&#39; || $danwei[$danwei_len - $left_num_len + $i] == &#39;万&#39; || $danwei[$danwei_len - $left_num_len + $i] == &#39;圆&#39;))) {
            $daxie = $daxie . $dint[$key_] . $danwei[$danwei_len - $left_num_len + $i];
        } else {
            //否则就不含单位
            $daxie = $daxie . $dint[$key_];
        }
    }

    //循环计算角分
    for($i = 0; $i < $right_num_len; $i++) {
        $key_ = substr($right_num, $i, 1);
        if($key_ > 0) {
            $daxie = $daxie . $dint[$key_] . $danwei1[$i];
        }
    }

    //计算转换后的长度
    $daxie_len = strlen($daxie);
    //设置文字切片从0开始,utf-8汉字占3个字符
    $j = 0;
    while($daxie_len > 0) {
        //每次切片两个汉字
        $str = substr($daxie, $j, 6);
        //判断切片后的文字不等于零万、零圆、零亿、零零
        if($str == &#39;零万&#39; || $str == &#39;零圆&#39; || $str == &#39;零亿&#39; || $str == &#39;零零&#39;) {
            //重新切片
            $left = substr($daxie, 0, $j);
            $right = substr($daxie, $j + 3);
            $daxie = $left . $right;
        }
        $j += 3;
        $daxie_len -= 3;
    }

    return &#39;人民币&#39; . $daxie . &#39;整&#39;;
}

$result = inttod(99090909090.19);
var_dump($result);

Running results:

How to convert numbers to uppercase in php

The source code has been uploaded to GitHub: https://github.com/cuiyuanxin/php-demo/blob /master/capital.php

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to convert numbers to uppercase in php. For more information, please follow other related articles on the PHP Chinese website!

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.