A function of Arabic numerals to Chinese numerals

Source: Internet
Author: User
Tags array
function | Chinese recently wrote a "Arabic numeral to Chinese digit" function. Search the elite area and see a similar one.


feel my algorithm is good, so posted out to share


If you want to use the conversion of the amount, the processing of the decimal part should be modified


<?php


function Ch_num ($num, $mode =true) {


$char = Array ("0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine");


$DW = Array ("", "Pick Up", "Bai", "Qian", "", "Million", "billion", "trillion");


$dec = "dot";


$retval = "";





if ($mode)


Preg_match_all ("/^0*" (\d*) \.? (\d*)/", $num, $ar);


Else


Preg_match_all ("/(\d*) \.?" (\d*)/", $num, $ar);





if ($ar [2][0]!= "")


$retval = $dec. Ch_num ($ar [2][0],false); If there are decimals, first recursively handle decimal


if ($ar [1][0]!= "") {


$str = Strrev ($ar [1][0]);


for ($i =0; $i <strlen ($STR); $i + +) {


$out [$i] = $char [$STR [$i]];


if ($mode) {


$out [$i]. = $str [$i]!= "0"? $DW [$i%4]: "";


if ($str [$i]+ $str [$i-1] = = 0)


$out [$i] = "";


if ($i%4 = 0)


$out [$i]. = $DW [4+floor ($i/4)];


}


}


$retval = Join ("", Array_reverse ($out)). $retval;


}


return $retval;


}





//echo ch_num ("12345006789001.123");


//echo ch_num ("880079.1234");


Echo ch_num ("300045.0123");





?>








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.