Convert Chinese positive integers to Arabic numerals data

Source: Internet
Author: User

In the processing of data, we often encounter positive integer Chinese data, in the storage of the database, we tend to store as Arabic numerals, because the Arabic numerals are convenient statistics and subsequent calculations. For example, for Chinese price data, it is reasonable to store the data in Arabic numerals.

In writing this program, I searched the online code written by PHP, feeling that most of the extensibility and compatibility is not particularly good, so simply wrote a:

1 functionToalbsingnumber ($Num) {2     Switch($Num) {3          Case' One ':4             return1;5          Case' Two ':6             return2;7          Case' Three ':8             return3;9          Case' Four ':Ten             return4; One          Case' Five ': A             return5; -          Case' VI ': -             return6; the          Case' Seven ': -             return7; -          Case' Eight ': -             return8; +          Case' Nine ': -             return9; +         default: A             return0; at     } - } - functionToalblevel ($Level) { -     Switch($Level) { -          Case' Billion ': -             return10000; in          Case' Million ': -             return10000; to          Case' Thousand ': +             return1000; -          Case' Hundred ': the             return100; *          Case' Ten ': $             return10;Panax Notoginseng         default: -             return1; the     } + } A functionToalbnumber ($str) { the     $money= 0; +     $preLevel= 10000000000000000000000000;//a default maximum value -     $level= "/(.)? (Million | thousand | hundred | 10)/U ";//question mark in order to be compatible with the magnitude of $     $last= "/[123456789]$/u"; $     Preg_match($last,$str,$SingLast);//Digit -     Preg_match_all($level,$str,$arr);//High -level segmentation -      for($i= 0;$i<Count($arr[1]);$i++) { the         $albLevel=toalblevel ($arr[2] [$i]); -         $albSingNum=toalbsingnumber ($arr[1] [$i]);Wuyi         if($albLevel>$preLevel) { the             $money=($money+$albSingNum)*$albLevel; -             $preLevel=$albLevel; Wu}Else { -             $money=$money+$albSingNum*$albLevel; About             $preLevel=$albLevel; $         } -          -     } -     if(isset($SingLast[0])) { A         $money+=toalbsingnumber ($SingLast[0]); +     } the     return $money; -}
Toalbnumber is the primary function, and the calls to Toalblevel and Toalbsingnumber are functions that get the relevant units and values.
The idea of writing this function is relatively simple. We analyze a data: 12,523,841.
123456789 is the basic number, converted into Arabic numerals will be each digit, but implementing this is not a bit on the number, but the point is indicated.
So I can use regular expressions to take out all the values on each and every bit, then multiply and add them according to the corresponding bit relationships.
This will allow you to complete the task.
In terms of detail, you can see:
The default maximum number of bits I use and the value of the previous one needs to be preserved. In response to the data in the preceding example, we know that the normal situation we are going to do is to multiply the bits and values.
The above data is sliced in the following way:
Thousand 1*1000
200 2*100
Fifty 5*10
20,000 2*10000
3,000 3*1000
......

This way, you can see that there is a problem in the 20,000 position. So we want to keep the previous bit and the next bit for comparison, once the previous bit is less than the next bit, then we can not simply put the value of the front sums and the subsequent number and bits of the product plus.
The correct operation should be to add the front and back numbers and multiply the digits in the back.
Therefore, the correct addition should be: (1*1000+2*100+5*10+2) *10000.
I have no problem with the rough test, basically I have not encountered more than single digits of data error, which is the data I have to deal with can allow the error.
Basically, this is not a complicated problem, as long as you want to understand the logic of the Chinese numeral description, it can correspond to the Arabic numerals commonly used in mathematics.
If you have a more simple method, please feel free to advise.

Convert Chinese positive integers to Arabic numerals data

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.