PHP replaces number 1.1 billion with kanji, for example, 150 turns 150.

Source: Internet
Author: User
PHP converts the number 1.1 billion to a kanji representation, such as 150 turns 150

Directly on the instance

Written to hundreds of billions.

/**

* @author ja Ode
* Replace the number 1.1 billion with Chinese characters, such as:123-> 123
* @param [num] $num [number]
* @return [string] [string]
*/
function Numtoword ($num)
{
$chiNum = Array (' 0 ', ' one ', ' two ', ' three ', ' four ', ' V ', ' VI ', ' VII ', ' VIII ', ' IX ');
$chiUni = Array (' ', ' ten ', ' hundred ', ' thousand ', ' million ', ' billion ', ' ten ', ' hundred ', ' thousand ');

$CHISTR = ";


$num _str = (string) $num;

$count = strlen ($num _str);
$last _flag = true; Whether the previous is 0
$zero _flag = true; Whether the first one
$temp _num = null; Provisional numbers

$CHISTR = ";//Stitching result
if ($count = = 2) {//two digits
$temp _num = $num _str[0];
$CHISTR = $temp _num = = 1? $chiUni [1]: $chiNum [$temp _num]. $chiUni [1];
$temp _num = $num _str[1];
$chiStr. = $temp _num = = 0? ": $chiNum [$temp _num];
}else if ($count > 2) {
$index = 0;
for ($i = $count-1; $i >= 0; $i-) {
$temp _num = $num _str[$i];
if ($temp _num = = 0) {
if (! $zero _flag &&! $last _flag) {
$CHISTR = $chiNum [$temp _num]. $CHISTR;
$last _flag = true;
}
}else{
$CHISTR = $chiNum [$temp _num]. $chiUni [$index%9]. $chiStr;

$zero _flag = false;
$last _flag = false;
}
$index + +;
}
}else{
$CHISTR = $chiNum [$num _str[0]];
}
return $chiStr;
}


$num = 150;
echo Numtoword ($num);

  • Related Article

    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.