Convert Arabic numerals into Chinese

Source: Internet
Author: User

Source publishing: http://darkbull.net/101/%E5%B0%86%E9%98%BF%E6%8B%89%E4%BC%AF%E6%95%B0%E5%AD%97%E8%BD%AC%E6%8D%A2%E6%88%90%E4%B8%AD%E6%96%87

At home on weekends, I wrote a small program to convert Arabic numerals into uppercase Chinese. The program has not been optimized, and has gone through detailed tests. It is mounted to the internet for use directly when necessary in the future.

Source code
Test code

#! /Usr/bin/Python #-*-encoding: UTF-8-*-import typesclass notintegererror (exception): passclass outofrangeerror (exception): pass_mapping = (U '0 ', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8 ', u'9',) _ p0 = (u'', u'10', u'bai', u'qi',) _ S4, _ S8, _ S16 = 10 ** 4, 10 ** 8, 10 ** 16_min, _ max = 0, 999999999999999999def _ to_chinese4 (Num): ''' conversion [0, 10000) between the Arabic numerals '''assert (0 <= num and num <_ S4) If num <10: Return _ mapping [num] else: lst = [] While num> = 10: lst. append (Num % 10) num = num/10 lst. append (Num) C = Len (LST) # number of digits result = u'' for idx, Val in enumerate (LST): If Val! = 0: Result + = _ P0 [idx] + _ mapping [Val] If idx <c-1 and LST [idx + 1] = 0: result + = u'0' return result [:-1]. replace (U '10 ', u '10') def _ to_chinese8 (Num): assert (Num <_ S8) to4 = _ to_chinese4 if num <_ S4: return to4 (Num) else: mod = _ S4 high, low = num/MoD, num % mod if low = 0: Return to4 (high) + u'wan' else: if low <_ S4/10: Return to4 (high) + u 'Ten thousand 0' + to4 (low) else: Return to4 (high) + u 'Ten thousand' + To4 (low) def _ to_chinese16 (Num): assert (Num <_ S16) to8 = _ to_chinese8 mod = _ S8 high, low = num/MoD, num % mod if low = 0: Return to8 (high) + u 'yie' else: If low <_ S8/10: Return to8 (high) + u 'yi0' + to8 (low) else: Return to8 (high) + u 'yie' + to8 (low) def to_chinese (Num): If type (Num )! = Types. inttype and type (Num )! = Types. longtype: Raise notintegererror (u '% s is not a integer. '% num) If num <_ min or num> _ max: Raise outofrangeerror (U' % d out of range [% d, % d)' % (Num, _ min, _ max) If num <_ S4: Return _ to_chinese4 (Num) Elif num <_ S8: Return _ to_chinese8 (Num) else: Return _ to_chinese16 (Num) if _ name _ = '_ main _': Print to_chinese (9000)

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.