PHP价格转大写

本文阅读 1 分钟
首页 PHP笔记 正文
  1. <?php
  2. function currencyToChinese($data)
  3. {
  4. $data&nbsp;=&nbsp;str_replace(&#39;,&#39;,&nbsp;&#39;&#39;,&nbsp;$data);
  5. if ($data == '') {
  6. return '';
  7. }
  8. $prefix = '';
  9. $cncap = '';
  10. if ($data < 0) {
  11. $prefix .= '负';
  12. $data&nbsp;=&nbsp;substr($data, 1, strlen($data) - 1);
  13. }
  14. $capnum = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
  15. $capdigit = array("", "拾", "佰", "仟");
  16. $subdata&nbsp;=&nbsp;explode(&quot;.&quot;,&nbsp;$data);
  17. if (!isset($subdata[1])) {
  18. $subdata[1] = 0;
  19. }
  20. if (intval($subdata[0])&nbsp;==&nbsp;0&nbsp;&amp;&amp;&nbsp;intval($subdata[1]) == 0) {
  21. return $prefix . "零元整";
  22. }
  23. $yuan&nbsp;=&nbsp;$subdata[0];
  24. $j&nbsp;=&nbsp;0;&nbsp;$nonzero = 0;
  25. for ($i&nbsp;=&nbsp;0;&nbsp;$i < strlen($subdata[0]);&nbsp;$i++) {
  26. if (0 == $i) { //确定个位
  27. if (intval($subdata[1]) != 0) {
  28. if (substr($subdata[0], -1, 1) == 0) {
  29. $cncap .= "元零";
  30. } else {
  31. $cncap .= "元";
  32. }
  33. } else {
  34. if (intval($subdata[0]) != 0) {
  35. $cncap = "元";
  36. }
  37. }
  38. }
  39. if (4 == $i) {
  40. $j = 0;
  41. $nonzero = 0;
  42. $cncap&nbsp;=&nbsp;&quot;万&quot;&nbsp;.&nbsp;$cncap;
  43. } //确定万位
  44. if (8 == $i) {
  45. $j = 0;
  46. $nonzero=0;
  47. $cncap=&quot;亿&quot;&nbsp;.&nbsp;$cncap;
  48. } //确定亿位
  49. $numb&nbsp;=&nbsp;substr($yuan, -1, 1); //截取尾数
  50. $cncap&nbsp;=&nbsp;($numb) ?
  51. $capnum[$numb] . $capdigit[$j] . $cncap&nbsp;:&nbsp;(($nonzero)?"零" . $cncap&nbsp;:&nbsp;$cncap);
  52. $nonzero&nbsp;=&nbsp;($numb) ? 1 : $nonzero;
  53. $yuan&nbsp;=&nbsp;substr($yuan, 0, strlen($yuan)-1); //截去尾数
  54. $j++;
  55. }
  56. $chiao = '';
  57. $cent = '';
  58. if (intval($subdata[1]) != 0) {
  59. $chiao&nbsp;=&nbsp;(substr($subdata[1], 0, 1)) ?
  60. $capnum[substr($subdata[1], 0, 1)] . "角" : "零";
  61. $cent&nbsp;=&nbsp;(substr($subdata[1], 1, 1)) ?
  62. $capnum[substr($subdata[1], 1, 1)] . "分" : "零分";
  63. }
  64. $cncap&nbsp;.=&nbsp;$chiao.$cent;
  65. if (substr($subdata[1], -1, 1) === '0') {
  66. $cncap .= "整";
  67. }
  68. $cncap&nbsp;=&nbsp;preg_replace(&quot;/(零)+/&quot;,&nbsp;&quot;\\1&quot;,&nbsp;$cncap); //合并连续“零”
  69. if (intval($subdata[0]) == 0) {
  70. $cncap&nbsp;=&nbsp;preg_replace(&quot;/(元零)+/&quot;,&nbsp;&quot;&quot;,&nbsp;$cncap);
  71. $cncap&nbsp;=&nbsp;preg_replace(&quot;/(元)+/&quot;,&nbsp;&quot;&quot;,&nbsp;$cncap);
  72. }
  73. if (intval($subdata[1])&nbsp;==&nbsp;0&nbsp;||&nbsp;substr($subdata[1], -1, 1) === '0') {
  74. $cncap&nbsp;=&nbsp;preg_replace(&quot;/(零分)+/&quot;,&nbsp;&quot;&quot;,&nbsp;$cncap);
  75. }
  76. return $prefix&nbsp;.&nbsp;$cncap;
  77. }
  78. ?>
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

HTML5的离线储存怎么使用及工作原理?
« 上一篇 05-15
响应式后台主题UI框架(H+4.9)带完整开发文档
下一篇 » 05-29

发表评论

惪特博客
  • 文章总数:
    18497 篇
  • 评论总数:
    53341 条
  • 标签总数:
    8873 个
  • 总浏览量:
    22946034 次
  • 最后更新:
    3月27日

最多点赞

随便看看

标签TAG