【面试题】小数转换为人民币大写形式,PHP实现。
2016-04-16 00:00
501 查看
前段时间面试,有个编程题目是将小数转换为人民币大写形式,最近用PHP多,就写一段,全当记个日志吧。未完待续。
<?php $newline = '<br/>'; $RMB = array('', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); $RMBUnit = array('圆', '拾', '百', '千', '万', '拾', '百', '千', '亿', '拾', '百', '千', '万', '拾', '百', '千'); function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } function change2RMB($input) { global $newline, $RMB, $RMBUnit; $inputStr = strval(number_format($input, 2)); $inputStr = str_replace(',', '', $inputStr); echo $inputStr . $newline; $inputExplode = explode('.', $inputStr, 2); $inputIntStr = $inputExplode[0]; $inputIntLen = strlen($inputIntStr); // echo $inputIntStr . $newline . $inputIntLen . $newline; if ($inputIntLen > 16) { return '你的钱已经超过1亿亿,请赞助作者完善此程序,感谢!'; } for ($i = 0; $i < $inputIntLen; $i++) { $j = $inputIntLen - $i - 1; $number = $RMB[$inputIntStr[$j]]; if ($number == '') { if ($i != 0 && $i != 4 && $i !=8) continue; } $resultIntStr = $number . $RMBUnit[$i] . $resultIntStr; } // echo $resultIntStr . $newline; $inputFloatStr = $inputExplode[1]; if ($inputFloatStr[0] != 0) { $resultFloatStr = $RMB[$inputFloatStr[0]] . '角'; } if ($inputFloatStr[1] != 0) { $resultFloatStr .=$RMB[$inputFloatStr[1]] . '分'; } return $resultIntStr . $resultFloatStr; } $input = randomFloat(0, 10000000000000000); // $input = 3000000000070.913; $result = change2RMB($input); echo $input . ' => ' . $result;
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- php7 读取php.ini[4]
- 只有程序员看的懂的面试圣经|如何拿下编程面试
- 下一次技术面试时要问的 3 个重要问题
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- C#实现将千分位字符串转换成数字的方法