PHP algorithm-Converts a numeric amount to an uppercase amount

Source: Internet
Author: User

Recently looking at some of the PHP algorithm problems, encountered a digital amount converted to uppercase amount of small algorithm problem, here to post an example of their own.

Note: This small algorithm applies to amounts within 100,000.

<?php//$num = 12345.67;functionRmb_upper ($num){    $num=round($num, 2);//take two decimal places    $num= '.$num;//Convert to Digital    $arr=Explode(‘.‘,$num); $str _left=$arr[0];//12345    $str _right=$arr[1];// the    $len _left=strlen($str _left);//the length to the left of the decimal    $len _right=strlen($str _right);//the length//loop to the right of the decimal point converts the string to an array,     for($i= 0;$i<$len _left;$i++)    {        $arr _left[] =substr($str _left,$i, 1); }    //Print_r ($arr _left); Output:array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5)     for($i= 0;$i<$len _right;$i++)    {        $arr _right[] =substr($str _right,$i, 1); }    //Print_r ($arr _right); Output:array ([0] = 6 [1] = 7)//construct array $daxie    $daxie=Array(        ' 0 ' = ' 0 ', ' 1 ' = ' one ', ' 2 ' + ', ' 3 ' = ' three ', ' 4 ' = ' ', ' 5 ' = ', ' 6 ' ' + ' land ', ' 7 ' = ' seven ', ' 8 ' = ' ' ba ', ' 9 ' = ' nine ',    ); //loops Replace the values in the array $arr_left with uppercase    foreach($arr _left  as $k=$v)    {        $arr _left[$k] =$daxie[$v]; Switch($len _left--)        {            //value is appended to the amount unit             Case5:$arr _left[$k]. = ' million '; Break;  Case4:$arr _left[$k]. = ' thousand '; Break;  Case3:$arr _left[$k]. = ' hundred '; Break;  Case2:$arr _left[$k]. = ' ten '; Break; default:$arr _left[$k]. = ' Yuan '; Break; }    }    //Print_r ($arr _left); Output:array ([0] = one million [1] = [+] [2] = three hundred [3] = ten [4] = dollar)    foreach($arr _right  as $k=$v)    {        $arr _right[$k] =$daxie[$v]; Switch($len _right--)        {             Case2:$arr _right[$k]. = ' angle '; Break; default:$arr _right[$k]. = ' points '; Break; }    }    //Print_r ($arr _right); Output:array ([0] = land angle [1] = seven)//convert the array into strings and stitch together    $new _left_str=implode(‘‘,$arr _left); $new _right_str=implode(‘‘,$arr _right); $new _str=$new _left_str.$new _right_str; //echo $new _str; Output: ' One million and three hundred ten Wu Yuanlu cents '//If the amount is 0, the uppercase string will have ' 10 ', such a string, need to replace    $new _str=Str_replace(' 0 ', ' 0 ',$new _str); $new _str=Str_replace(' 0 ', ' 0 ',$new _str); $new _str=Str_replace(' 0 ', ' 0 ',$new _str); $new _str=Str_replace(' 10 ', ' 0 ',$new _str); $new _str=Str_replace(' 000 ', ' 0 ',$new _str); $new _str=Str_replace(' 00 ', ' 0 ',$new _str); $new _str=Str_replace(' 0 yuan ', ' yuan ',$new _str); //echo ' <br/> ';    return $new _str;}EchoRmb_upper (12345.67);

PHP algorithm-Converts a numeric amount to an uppercase amount

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.