Home Backend Development PHP Tutorial php实现将人民币金额转大写的方法类

php实现将人民币金额转大写的方法类

Jun 20, 2016 pm 01:03 PM
data self ZERO

工作中偶尔会碰到需要将人民币金额,也即阿拉伯数字转化为大写汉字的这种情况,下面是作者经过实践总结出来的方法,特此记录以防备忘。

1

<p>class Num2Cny{</p>  static $basical=array(0=>'零','壹','贰','叁','肆','伍','陆','柒','捌','玖');<br />  static $advanced=array(1=>'拾','佰','仟');<br />  public static function ParseNumber($number){<br />    $number=trim($number);<br />    if(!is_numeric($number)||intval($number)>999999999999) return 'error';<br />    if($number==0) return '零';<br />    if(strpos($number,'.')){<br />      $number=round($number,2);<br />      $data=explode('.',$number);<br />      $data[0]=self::int($data[0]);<br />      $data[1]=self::dec($data[1]);<br />      return $data[0].$data[1];<br />    }else{<br />      return self::int($number).'整';<br />    }<br />  }<br />  public static function int($number){<br />    $arr=array_reverse(str_split($number));<br />    $data='';<br />    $zero=false;<br />    $zero_num=0;<br />    foreach($arr as $k=>$v){<br />      $_chinese='';<br />      $zero=($v==0)?true:false;<br />      $x=$k%4;<br />      if($x && $zero && $zero_num>1)continue;<br />      switch($x){<br />        case 0:<br />          if($zero){<br />            $zero_num=0;<br />          }else{<br />            $_chinese=self::$basical[$v];<br />            $zero_num=1;<br />          }<br />          if($k==8){<br />            $_chinese.='亿';<br />          }elseif($k==4){<br />            $_chinese.='万';<br />          }<br />          break;  <br />        default:<br />          if($zero){<br />            if($zero_num==1){<br />              $_chinese=self::$basical[$v];<br />              $zero_num++;<br />            }<br />          }else{<br />            $_chinese=self::$basical[$v];<br />            $_chinese.=self::$advanced[$x];<br />          }<br />      }<br />      $data=$_chinese.$data;<br />    }<br />    return $data.'元';<br />  }<br />  public static function dec($number){<br />    if(strlen($number)<2) $number.='0';<br />    $arr=array_reverse(str_split($number));<br />    $data='';<br />    $zero_num=false;<br />    foreach($arr as $k=>$v){<br />      $zero=($v==0)?true:false;<br />      $_chinese='';<br />      if($k==0){<br />        if(!$zero){<br />          $_chinese=self::$basical[$v];<br />          $_chinese.='分';<br />          $zero_num=true;<br />        }<br />      }else{<br />        if($zero){<br />          if($zero_num){<br />            $_chinese=self::$basical[$v];<br />          }<br />        }else{<br />          $_chinese=self::$basical[$v];<br />          $_chinese.='角';<br />        }<br />      }<br />      $data=$_chinese.$data;<br />    }<br />    return $data;<br />  }<br /><p>}

Copy after login

使用过程也很简单,如下:

echo Num2Cny::ParseNumber(1234567.5);//www.scutephp.com

结果将输出:

壹佰贰拾叁万肆仟伍佰陆拾柒元伍角

相关:JS数字金额转大写金额方法


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)

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

How to use self in Python How to use self in Python May 17, 2023 pm 10:40 PM

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python. We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates, such as abstract things like students. , can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different. 1. Take the Student class as an example. In Python, the class is defined as follows: classStudent(object):pass(Object) indicates which class the class inherits from. The Object class is all

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

What to do if mysql load data is garbled? What to do if mysql load data is garbled? Feb 16, 2023 am 10:37 AM

The solution to the garbled mysql load data: 1. Find the SQL statement with garbled characters; 2. Modify the statement to "LOAD DATA LOCAL INFILE "employee.txt" INTO TABLE EMPLOYEE character set utf8;".

What are the differences between xdata and data What are the differences between xdata and data Dec 11, 2023 am 11:30 AM

The differences are: 1. xdata usually refers to independent variables, while data refers to the entire data set; 2. xdata is mainly used to establish data analysis models, while data is used for data analysis and statistics; 3. xdata is usually used for regression Analysis, variance analysis, predictive modeling, data can be analyzed using various statistical methods; 4. xdata usually requires data preprocessing, and data can contain complete original data.

More returns than sales: The Humane Ai Pin is becoming a commercial disaster More returns than sales: The Humane Ai Pin is becoming a commercial disaster Aug 08, 2024 pm 01:14 PM

Shortly after the launch of the Humane Ai Pin, scathing reviews revealed that the AI gadget was anything but ready for the market, as most of the originally advertised features either didn't work properly or were simply missing, the battery life was

Can't data in vue component be a function? Can't data in vue component be a function? Dec 19, 2022 pm 05:22 PM

No, data in the vue component must be a function. Components in Vue are used for reuse. In order to prevent data reuse, they are defined as functions. The data data in the vue component should be isolated from each other and not affect each other. Every time the component is reused, the data data should be copied once. Later, when the data data in the component is changed in a reused place, other data will be copied. If the data data of reused local components is not affected, you need to return an object as the status of the component through the data function.

AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems Aug 31, 2024 am 12:59 AM

Everyone and their aunt seem to be hopping aboard the AI train in search of inflated profit margins and marketing hype — just look at AMD's recent Ryzen rebrand as a prime example of this AI hype. A recent study conducted by RAND has found that this

See all articles