How to implement an example of how to convert Chinese numerals into Arabic numerals in python

Source: Internet
Author: User
This article mainly introduces the method of Python to convert Chinese numerals into Arabic numerals, involving Python string traversal, conversion related operation skills, the need for friends can refer to the following

In this paper, the method of translating Chinese numerals into Arabic numerals is described. Share to everyone for your reference, as follows:

First, the demand

3,200 lines of code were written today. 3200 lines of code were written today.

The two lines mean the same, but the expression is not very able to be unified out.

Second, the principle

Numbers are characterized by numbers + units, such as 300, 42, 9,002
Can be traversed from the back, encountered a number of 0 to 9, multiplied by the previous unit, encountered a new unit (implementing) replaced by the number for the next number.

Iii. examples

571. Three-->3 3 <10:total = 32. Ten-->10, 10≥10, and not 0:r = 103. Four-->4, 4<10:total = 3 + 4*10 = 434. Hundred-->100, 10 0≥10, and not 0:r = 1005. Five-->5, 5<10:total = 43 + 5*100 = 543

Iv. Reference Code

#-*-coding:cp936-*-import reimport stringcommon_used_numerals_tmp ={' 0 ': 0, ' one ': 1, ' two ': 2, ' both ': 2, ' three ': 3, ' four ': 4, ' five ': 5, ' six ': 6, ' seven ': 7, ' eight ': 8, ' nine ': 9, ' ten ': 10, ' hundred ': 100, ' thousand ': 1000, ' million ': 10000, ' billion ': 100000000}common_used_numerals = {}for key in Common_u Sed_numerals_tmp:common_used_numerals[key.decode (' cp936 ')] = Common_used_numerals_tmp[key]def Chinese2digits ( Uchars_chinese): total = 0 R = 1 #表示单位: Chichong ... for i in range (len (Uchars_chinese)-1,-1,-1): val = C Ommon_used_numerals.get (Uchars_chinese[i]) if Val >= and i = = 0: #应对 44 * like if val > r:r = V  Al total = Total + val else:r = R * Val #total =total + R * x elif val >= 10:if val > r:r = val else:r = r * Val else:total = total + R * Val return totalprint Chinese2dig Its (' 232 '. Decode (' cp936 ')) print "-------------------------" Print chinese2digits (' 12 '. Decode (' cp936 ')) print "--- ----------------------"Print chinese2digits (' 100,080,323 '. Decode (' cp936 ')) 

Results:

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.