Home > Backend Development > PHP Tutorial > Convert php numbers to Chinese characters amount

Convert php numbers to Chinese characters amount

WBOY
Release: 2016-07-25 08:43:04
Original
923 people have browsed it
  1. /**
  2. * Convert numbers to Chinese
  3. * @param string|integer|float $num target number
  4. * @param integer $mode mode [true: amount (default), false: ordinary number representation]
  5. * @param boolean $sim use lowercase (Default)
  6. * @return string
  7. */
  8. function number2chinese($num,$mode = true,$sim = true){
  9. if(!is_numeric($num)) return 'contains non-numbers Non-decimal point character! ';
  10. $char = $sim ? array('zero','one','two','three','four','five','six','seven','eight','nine' )
  11. : array('zero','one','two','three','four','five','Lu','旒','捌','玖');
  12. $unit = $sim ? array('','十','hundred','thousand','','ten thousand','billion','trillion')
  13. : array('','十','hundred', '仟','','万','hundred million','trillion');
  14. $retval = $mode ? 'Yuan':'point';
  15. //Decimal part
  16. if(strpos($num, ' .')){
  17. list($num,$dec) = explode('.', $num);
  18. $dec = strval(round($dec,2));
  19. if($mode){
  20. $retval .= "{$char[$dec['0']]}angle {$char[$dec['1']]}cent";
  21. }else{
  22. for($i = 0,$c = strlen( $dec);$i < $c;$i++) {
  23. $retval .= $char[$dec[$i]];
  24. }
  25. }
  26. }
  27. //Integer part
  28. $str = $mode ? strrev (intval($num)) : strrev($num);
  29. for($i = 0,$c = strlen($str);$i < $c;$i++) {
  30. $out[$i] = $char[$str[$i]];
  31. if($mode){
  32. $out[$i] .= $str[$i] != '0'? $unit[$i%4] : '' ;
  33. if($i>1 and $str[$i]+$str[$i-1] == 0){
  34. $out[$i] = '';
  35. }
  36. if($i%4 = = 0){
  37. $out[$i] .= $unit[4+floor($i/4)];
  38. }
  39. }
  40. }
  41. $retval = join('',array_reverse($out)) . $ retval;
  42. return $retval;
  43. }
  44. //Instance call==================================== ==================
  45. $num = '0123648867.789';
  46. echo $num,'
    ';
  47. //Chinese character representation of ordinary numbers
  48. echo 'Ordinary :',number2chinese($num,false),'';
  49. echo '
    ';
  50. //Amount expressed in Chinese characters
  51. echo 'Amount (simplified):',number2chinese($num,true),'';
  52. echo '
    ';
  53. echo 'Amount (Traditional):',number2chinese($num,true,false);
  54. ?>
Copy code

php


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template