search
Homephp教程php手册php实现将人民币金额转大写的办法

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

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

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

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

结果将输出:

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

另外说明一下的是如果仅需要将阿拉伯数字转化为大写的情况下,可参照本站文章:

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



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
为什么NameResolutionError(self.host, self, e) from e,怎么解决为什么NameResolutionError(self.host, self, e) from e,怎么解决Mar 01, 2024 pm 01:20 PM

报错的原因NameResolutionError(self.host,self,e)frome是由urllib3库中的异常类型,这个错误的原因是DNS解析失败,也就是说,试图解析的主机名或IP地址无法找到。这可能是由于输入的URL地址不正确,或者DNS服务器暂时不可用导致的。如何解决解决此错误的方法可能有以下几种:检查输入的URL地址是否正确,确保它是可访问的确保DNS服务器可用,您可以尝试在命令行中使用"ping"命令来测试DNS服务器是否可用尝试使用IP地址而不是主机名来访问网站如果是在代理

Python中的self怎么使用Python中的self怎么使用May 17, 2023 pm 10:40 PM

在介绍Python的self用法之前,先来介绍下Python中的类和实例我们知道,面向对象最重要的概念就是类(class)和实例(instance),类是抽象的模板,比如学生这个抽象的事物,可以用一个Student类来表示。而实例是根据类创建出来的一个个具体的“对象”,每一个对象都从类中继承有相同的方法,但各自的数据可能不同。1、以Student类为例,在Python中,定义类如下:classStudent(object):pass(Object)表示该类从哪个类继承下来的,Object类是所有

data文件夹里面是什么数据data文件夹里面是什么数据May 05, 2023 pm 04:30 PM

data文件夹里面是系统及程序的数据,比如软件的设置和安装包等,Data文件夹中各个文件夹则代表的是不同类型的数据存放文件夹,无论Data文件指的是文件名Data还是扩展名data,都是系统或程序自定义的数据文件,Data是数据保存的备份类文件,一般可以用meidaplayer、记事本或word打开。

mysql load data乱码怎么办mysql load data乱码怎么办Feb 16, 2023 am 10:37 AM

mysql load data乱码的解决办法:1、找到出现乱码的SQL语句;2、修改语句为“LOAD DATA LOCAL INFILE "employee.txt" INTO TABLE EMPLOYEE character set utf8;”即可。

xdata和data有哪些区别xdata和data有哪些区别Dec 11, 2023 am 11:30 AM

区别有:1、xdata通常指的是自变量,data则是指整个数据集;2、xdata主要用于建立数据分析模型,data则是用于进行数据分析和统计;3、xdata通常用于回归分析、方差分析、预测建模,data则可以使用各种统计方法进行分析;4、xdata通常需要进行数据预处理,data则可以包含完整的原始数据。

AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problemsAI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problemsAug 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

MySQL 狂写错误日志MySQL 狂写错误日志Feb 18, 2024 pm 05:00 PM

一台核心业务数据库,版本为MySQL8.34社区服务器版。从上线以来,这个数据库服务器的错误日志增增加非常迅猛(如下图所示),每24小时能增加到10多个G的容量。因为有故障报警,也还没有影响到业务的正常访问,有关人员不让重启MySQL服务。鉴于这个情况,我只好设置一个自动计划任务,在每晚的夜间定点清理这些日志。具体的操作时候在系统命令行,执行“crontab-e”,添加如下的文本行:0001***echo>/data/mysql8/data/mysql_db/mysql.log保存并退出编辑模式

vue组件中data不能是函数吗vue组件中data不能是函数吗Dec 19, 2022 pm 05:22 PM

不是,vue组件中data必须是一个函数。vue中组件是用来复用的,为了防止data复用,将其定义为函数。vue组件中的data数据都应该是相互隔离,互不影响的,组件每复用一次,data数据就应该被复制一次,之后,当某一处复用的地方组件内data数据被改变时,其他复用地方组件的data数据不受影响,就需要通过data函数返回一个对象作为组件的状态。

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools