Python to convert Chinese numbers to Arabic numerals,

Source: Internet
Author: User

Python to convert Chinese numbers to Arabic numerals,

This article describes how to convert Chinese numbers to Arabic numerals using Python. We will share this with you for your reference. The details are as follows:

I. Requirements

Today I wrote three thousand two hundred lines of code. Today I wrote 3200 lines of code.

The two rows have the same meaning, but they are not the same expression.

Ii. Principles

Number features are numbers + units, such as three hundred, nine thousand and two
You can traverse from the back to the front. If you encounter a number ranging from 0 to 9, multiply it by the unit of the previous digit. If you encounter a new unit (10 million), replace it with a number for the next digit.

Iii. Example

Five hundred and forty-three 1. 3 --> 3 3 <10: total = 32. 10 --> 10, 10 ≥ 10, and not 0: r = 103. 4 --> 4, 4 <10: total = 3 + 4*10 = 434. hundred --> 100, 10 0 ≥ 10, and not 0: r = 1005. 5 --> 5, 5 <10: total = 43 + 5*100 = 543

Iv. Reference Code

#-*-Coding: cp936-*-import reimport stringcommon_used_numerals_tmp = {'0': 0, '1': 1, '2': 2, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9 ': 9, '10': 10, '100': 100, '100': 1000, '100': 10000, '100 ': 100000000} common_used_numerals ={} for key in common_used_numerals_tmp: common_used_numerals [key. decode ('cp936')] = common_used_numerals_tmp [key] def chinese2digits (uchars_chinese): total = 0 r = 1 # indicates that the Unit is 10 thousand... for I in range (len (uchars_chinese)-1,-1,-1): val = common_used_numerals.get (uchars_chinese [I]) if val> = 10 and I = 0: # if val> r: r = val 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 chinese2digits ('20140901 '. decode ('cp936') print "-----------------------" print chinese2digits ('12 '. decode ('cp936') print "-----------------------" print chinese2digits ('1970, 0.1 billion, 80,323 '. decode ('cp936 '))

Result:

PS: Here are several digital conversion tools for your reference:

Online Conversion Tool for RMB capital:
Http://tools.jb51.net/zhuanhuanqi/rmbupper

Tools for translating online Arabic numerals into English:
Http://tools.jb51.net/transcoding/num2english

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.