Home Backend Development PHP Tutorial PHP将阿拉伯数字转化为汉字的方法

PHP将阿拉伯数字转化为汉字的方法

Jun 20, 2016 pm 01:05 PM
array char out str

有木有遇到财务相关的开发,好端端的阿拉伯数字,非得整成汉字形式显示,这也就罢了,还得整成繁体汉字。很苦恼吧。

下面将和你分享一下本人遇到需要利用PHP将阿拉伯数字转化为汉字时的处理办法,可以总结为一个函数,具体自己研究去吧。

<p>function num2char($num,$mode=true){</p>	$char = array('零','一','二','三','四','五','六','七','八','九');<br />	//$char = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖);<br />	$dw = array('','十','百','千','','万','亿','兆');<br />	//$dw = array('','拾','佰','仟','','萬','億','兆');<br />	$dec = '点';  //$dec = '點';<br />	$retval = '';<br />	if($mode){<br />		preg_match_all('/^0*(\d*)\.?(\d*)/',$num, $ar);<br />	}else{<br />		preg_match_all('/(\d*)\.?(\d*)/',$num, $ar);<br />	}<br />	if($ar[2][0] != ''){<br />		$retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数<br />	}<br />	if($ar[1][0] != ''){<br />		$str = strrev($ar[1][0]);<br />		for($i=0;$i<strlen($str);$i++) {<br />			$out[$i] = $char[$str[$i]];<br />			if($mode){<br />				$out[$i] .= $str[$i] != '0'? $dw[$i%4] : '';<br />				if($str[$i]+$str[$i-1] == 0){<br />					$out[$i] = '';<br />				}<br />				if($i%4 == 0){<br />					$out[$i] .= $dw[4+floor($i/4)];<br />				}<br />			}<br />		}<br />		$retval = join('',array_reverse($out)) . $retval;<br />	}<br />	return $retval;<br /><p>}</p>
Copy after login

使用示例:

echo '300045.0123:'.num2char('300045.0123');//www.scutephp.com


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 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

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)

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

What do out and in interfaces mean? What do out and in interfaces mean? Sep 28, 2021 pm 04:39 PM

The out interface refers to the output interface, and the in interface refers to the input interface. The out interface generally represents the audio source line output interface, which is used to connect loads, such as speakers, headphones, etc.; while the in interface generally represents the audio source line input interface, which is used to connect CD players, mobile phones, MP3 players, computers, etc.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Python built-in type str source code analysis Python built-in type str source code analysis May 09, 2023 pm 02:16 PM

1The basic unit of Unicode computer storage is the byte, which is composed of 8 bits. Since English only consists of 26 letters plus a number of symbols, English characters can be stored directly in bytes. But other languages ​​(such as Chinese, Japanese, Korean, etc.) have to use multiple bytes for encoding due to the large number of characters. With the spread of computer technology, non-Latin character encoding technology continues to develop, but there are still two major limitations: no multi-language support: the encoding scheme of one language cannot be used in another language and there is no unified standard: for example There are many encoding standards in Chinese such as GBK, GB2312, GB18030, etc. Since the encoding methods are not unified, developers need to convert back and forth between different encodings, and many errors will inevitably occur.

What should I do if my win10 system crashes and displays out of memory? What should I do if my win10 system crashes and displays out of memory? Feb 09, 2024 pm 03:00 PM

Win10 system crashes and displays outofmemory. Recently, many users have encountered this prompt when using their computers, which requires frequent restarts to repair. So how should we deal with this situation? To address this problem, this issue’s win10 tutorial is here Share the complete operation steps with the majority of users, hoping to help more friends solve their problems. What to do if win10 system crashes and displays outofmemory 1. Right-click this computer on the desktop and select "Properties" in the option list. 2. After entering the new window interface, click the "Advanced System Settings" option in the upper left corner. 3. In the window that opens, switch to the "

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

See all articles