PHP数字金额转换成中文大写显示

yipeiwu_com6年前PHP代码库

整个功能其实不难,只是还算挺实用,所以记一下哈,其他编程语言转换一下也是能可以的!

思路:把传过来的金额转换成整数和小数两部分,再对其分别进行转换处理

代码附上:

function moneyToString($num)
{
  $digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  $radices =['', '拾', '佰', '仟', '万', '亿'];
  $bigRadices = ['', '万', '亿'];
  $decimals = ['角', '分'];
  $cn_dollar = '元';
  $cn_integer = '整';
  $num_arr = explode('.', $num);
  $int_str = $num_arr[0] ?? '';
  $float_str = $num_arr[1] ?? '';
  $outputCharacters = '';
  if ($int_str) {
    $int_len = strlen($int_str);
    $zeroCount = 0;
    for ($i = 0; $i < $int_len; $i++) {
      $p = $int_len - $i - 1;
      $d = substr($int_str, $i, 1);
      $quotient = $p / 4;
      $modulus = $p % 4;
      if ($d == "0") {
        $zeroCount++;
      }
      else {
        if ($zeroCount > 0)
        {
          $outputCharacters += $digits[0];
        }
        $zeroCount = 0;
        $outputCharacters .= $digits[$d] . $radices[$modulus];
      }
      if ($modulus == 0 && $zeroCount < 4) {
        $outputCharacters .= $bigRadices[$quotient];
        $zeroCount = 0;
      }
    }
    $outputCharacters .= $cn_dollar;
  }
  if ($float_str) {
    $float_len = strlen($float_str);
    for ($i = 0; $i < $float_len; $i++) {
      $d = substr($float_str, $i, 1);
      if ($d != "0") {
        $outputCharacters .= $digits[$d] . $decimals[$i];
      }
    }
  }
  if ($outputCharacters == "") {
    $outputCharacters = $digits[0] . $cn_dollar;
  }
  if ($float_str) {
    $outputCharacters .= $cn_integer;
  }
  return $outputCharacters;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【宜配屋www.yipeiwu.com】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

php中get_defined_constants函数用法实例分析

本文实例讲述了php中get_defined_constants函数用法。分享给大家供大家参考。具体分析如下: get_defined_constants ( PHP 4中 > =...

PHP Ajax中文乱码问题解决方法

是因为XMLHttp在处理返回的responstText的时候把responstBody按UTF-8编码进行解码的,如果服务器端送出的数据流的确是UTF-8编码,那么中文字就会正确显示,...

浅谈PHP中foreach/in_array的使用

php在开发效率很高,这是无可厚非的,但是却是在牺牲执行效率的。php数组功能非常强大,但是也要多加考虑,多试几种情况情况,以防万一,这里,我就简单的说两个遇到的坑,以后如果有发现更多的...

php类自动加载器实现方法

本文实例讲述了php类自动加载器实现方法。分享给大家供大家参考。具体如下: 这里autoload 可兼容以下格式: Cache_File_Json class_xxx.php xxx...

Wordpress php 分页代码

Wordpress php 分页代码

效果: 将下面的函数放到你的主题的 functions.php 文件中:复制代码 代码如下: function theme_echo_pagenavi(){ global $reque...