PHP实现金额数字转换成大写函数
<?php
header("Content-Type:text/html;charset=utf-8"); function num_to_upper($num)
{
$d = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
$e = array('元','拾','佰','仟','万','拾万','佰万','仟万','亿','拾亿','佰亿','仟亿','万亿');
$p = array('分','角');
$zheng='整'; //追加"整"字
$final = array(); //结果
$inwan=0; //是否有万
$inyi=0; //是否有亿
$len_pointdigit=0; //小数点后长度
$y=0;
if($c = strpos($num, '.')) { //有小数点,$c为小数点前有几位数
$len_pointdigit = strlen($num)-strpos($num, '.')-1; // 判断小数点后有几位数
if($c>13) { //简单的错误处理
echo "数额太大,已经超出万亿.";
die();
} elseif($len_pointdigit>2) { //$len_pointdigit小数点后有几位
echo "小数点后只支持2位.";
die();
}
} else { //无小数点
$c = strlen($num);
$zheng = '整';
}
for($i=0;$i<$c;$i++) { //处理整数部分
$bit_num = substr($num, $i, 1); //逐字读取 左->右
if($bit_num!=0 || substr($num, $i+1, 1)!=0) //当前是零 下一位还是零的话 就不显示
@$low2chinses = $low2chinses.$d[$bit_num];
if($bit_num || $i==$c-1)
@$low2chinses = $low2chinses.$e[$c-$i-1];
}
for($j=$len_pointdigit; $j>=1; $j--) { //处理小数部分
$point_num = substr($num, strlen($num)-$j, 1); //逐字读取 左->右
if($point_num != 0)
@$low2chinses = $low2chinses.$d[$point_num].$p[$j-1];
// if(substr($num, strlen($num)-2, 1)==0 && substr($num, strlen($num)-1, 1)==0) //小数点后两位都是0
}
$chinses = str_split($low2chinses,2); //字符串转换成数组
//print_r($chinses);
for($x=sizeof($chinses)-1;$x>=0;$x--) { //过滤无效的信息
if($inwan==0&&$chinses[$x]==$e[4]) { //过滤重复的"万"
$final[$y++] = $chinses[$x];
$inwan=1;
}
if($inyi==0&&$chinses[$x]==$e[8]) { //过滤重复的"亿"
$final[$y++] = $chinses[$x];
$inyi=1;
$inwan=0;
}
if($chinses[$x]!=$e[4]&&$chinses[$x]!=$e[8]) //进行整理,将最后的值赋予$final数组
$final[$y++] = $chinses[$x];
}
$newstring=(array_reverse($final)); //$final为倒数组,$newstring为正常可以使用的数组
$nstring=join($newstring); //数组变成字符串
if(substr($num,-2,1)==0 && substr($num,-1)<>0) { //判断原金额角位为0 ? 分位不为0 ?
$nstring=substr($nstring,0,(strlen($nstring)-4))."零".substr($nstring,-4,4); //这样加一个零字
}
$fen="分";
$fj=substr_count($nstring, $fen); //如果没有查到分这个字
return $nstring=($fj==0)?$nstring.$zheng:$nstring; //就将"整"加到后面
} echo num_to_upper(1234.56); ?>
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANsAAABICAIAAADwExf6AAAM7klEQVR4nO2d/XMbxRnH7+/ptNVMS2kpUGgpDEMDJcNbgSgNzgvBJKEmNIXQEDNQUvWHKgktQ0iigIPFyBbHCdlJHL1EliynTmLFjiW5ih3JcmxZL0bWSU5sSWHcH8467e3u7Z1kub4k+53nB2u1++ze7kfPvsizYmb4EmgTmXnWdZE7dyk6w2cKZZUWTxe6vJetjoFYugA5pEatJmNmciXRRuPfmbv95y6OpvlSJl+uydJ8yXd5/OMvu7/q9tt7L1OjVp9ViZxIzZ/81nt+JFYri6AFRqfM9l6+MH+LiqouMTN8eYYvJ3KlzrMDzoFwpnBrhdYbGLO5BsrlW0tUVLWLSeRKiVzJM3jV6hhI8aV0vrxys3kGB4YjDW9rJpNpuE8qrYmZzpWiqfkTnGcskUvly4p2rP3rzy02cp5ryXyb7Rw/f7OxbQ0Gg411SKVBMdO50tmB8Gn/FTU47j9g1P384R/f+/ChI23knM4LYc/FEKHicrkcCoW8Xi/HcRzHeb3eUChULpcJRSiRd4OYqbnSCc4TTRZS+Vtkaz1w8Be//t2651954tmNP33g0SRfJmSezNw8zrpuff89ttZ4PO7z+QKBwKVLl0ZGRgKBwOzsrMVicblcExMTcm2lRN4NYoaupb463Z/ky2Tbf8B432/Wrd+w7cWtf3ryxc3HTloVi3Q6BiamZ9Eq4/G42+1eXFzM5XKhUMhisZjN5q6uLrPZzLIsy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0A9dc/aeOEOvUEiie+LL9HjE/LowJ2EOkWpEQsSiVS1fcLFyNqIxI7ynRUjJRtk0qxSB5E1qSYiod8II3UVdKZIDGTggylM7zAuuPMoUoyUQqqSSPy+cLW+TMSuI9FdgOyMWBORoDPc0AF7K8wINfzwD/KvRCTYLEzUk3le3I4KwU6uDUpEoi1WNWujmWWJlD3eWbWvbapEqtm1oRDUQKQYUlDWpV8aQq9riJEqn0FmTMnrSMy7St/0oueLcFcgHSIuC1Q8AnFZDtRU+wF/5fSVQYM9vhsbqLv3v9Eqoy7ZMCutI6XvSD9ImK1o7YNWwxpEZseOnDbiPt0EqT2WXi3dvURSaVOUSCptiRJJpS1RIqm0JUoklbZEiaTSliiRVNoSJZJKW6JEUmlLlEgqbYkSSaUt/Q9HWwwsuwbwmwAAAABJRU5ErkJggg==" alt="" />
PHP实现金额数字转换成大写函数的更多相关文章
- 【PHP】金额数字转换成大写形式
<?php /*将数字金额转成大写*/ function num_to_upper($num) { $d = array('零','壹','贰','叁','肆','伍','陆','柒','捌', ...
- C# 数字转换成大写
/// <summary> /// 数字转大写 /// </summary> /// <param name="Num">数字</para ...
- js将数字转换成大写的人民币表达式
function changeNumMoneyToChinese(money) { var cnNums = new Array("零", "壹", " ...
- C#小写数字金额转换成大写人民币金额的算法
C#小写数字金额转换成大写人民币金额的算法 第一种方法: using System.Text.RegularExpressions;//首先引入命名空间 private string DaXie(st ...
- SQL函数:小写金额转换成大写
/********************************************************作者:版本:1.0创建时间:20020227修改时间:功能:小写金额转换成大写参数:n ...
- javascript 将数字(金额)转成大写
将计算好的金额转换成大写,这些功能非常多,下面我改进了一下代码(原文在这里:http://www.cnblogs.com/zsanhong/p/3509464.html). /** * _SetNum ...
- 算法--java实现将数字转换成人民币大写(迅雷面试题)
今天去迅雷面试,是个数字转换成人民币的算法题: public class Rmb { /** * 人民币的基本信息和操作 * * @author soyoungboy * @version 1.0 * ...
- js 将数字转换成人民币大写的方法
//将数字转换成人民币大写的方法 var digitUppercase = function (n) { var fraction = ['角', '分']; var digit = [ '零', ' ...
- C# 数字转换成汉字大写 数值转换成汉字大写
1.数字转换成汉字大写 public string NumToChinese(string x) { //数字转换为中文后的数组 //转载请注明来自 http://www.shang11.com st ...
随机推荐
- 【Android】6.1 Toast(信息提示框)
分类:C#.Android.VS2015: 创建日期:2016-02-08 一.简介 Toast用于向用户显示一些帮助或者提示信息.前面我们已经多次用到它,这里只是系统地将其总结一下,并演示它的各种基 ...
- css margin塌陷问题
一.同级块级元素塌陷 html <h2> 同级块级元素塌陷 </h2> <div class="block1"> block1 </div ...
- 每日英语:Why Are Items Pricier in China?
In China, consumers pay nearly $1 more for a latte at Starbucks than their U.S. counterparts. A Cadi ...
- linux命令(30):tail
linux ---tail命令 linux中tail命令---用于查看文件内容 最基本的是cat.more和less. 1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 /e ...
- Log4j常用配置及使用
Log4j常用配置及使用 2016-12-14 目录 1 添加log4j依赖2 使用代码配置并调用log 2.1 代码配置LogConfiguration.java 2.2 调用 2.3 结果3 ...
- encoding/path可能引起无数奇怪的问题
例如如下代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...
- Servlet打印HTML页面乱码问题
代码就在下面,最开始我以为是request和response的编码问题,还去搞了一个filter,结果最后发现跟filter半毛钱关系都没有,都是因为没有打印这么一句 out.println(&quo ...
- java 多线程6: 中断机制 优雅的终止java线程
前文 java 多线程5: java 终止线程及中断机制 (stop().interrupt() .interrupted().isInterrupted()) 使用 interrupt() 和 in ...
- C#中一道关于线程同步的练习题——模拟多窗口售票
题目:模拟窗口卖票,四个窗口同时对外开放售票,需要按顺序售出. 要求:输出每一张票的售出时间和售出窗口,不能出现票未售出或者被售出多次的情况. using System; using System.C ...
- IWDG—独立看门狗
本章参考资料:<STM32F4XX 中文参考手册> IWDG 章节.学习本章时,配合<STM32F4XX 中文参考手册> IWDG 章节一起阅读,效果会更佳,特别是涉及到寄存器 ...