zfoxatis

NumToWord (Eng,Hu)

Sep 17th, 2012
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3.2
  2. # -*- coding: Utf-8 -*-
  3.  
  4. '''create by: zfox, 17.09.2012'''
  5.  
  6.  
  7. class NumToWordHu():
  8.     '''Kiirja az numerikus egész számokat szöveges formában 1-től billió-1 -ig '''
  9.     def __init__(self,number):
  10.         self.number=int(number)
  11.         self.numbertext=''
  12.  
  13.         self.singles=['','egy','kettõ','három','négy','öt','hat','hét','nyolc','kilenc']
  14.         self.teens=['',['tíz','tizen'],['húsz','huszon'],'harminc','negyven','ötven', \
  15.                     'hatvan','hetven','nyolcvan','kilencven']
  16.        
  17.         for thousandtext,exp in [('milliárd',3),('millió',2),('ezer',1),('',0)]:
  18.             self.numbertext+=self.thousandgroup(thousandtext,1000**exp)
  19.    
  20.     def thousandgroup(self,thousandtext,divider):
  21.         hundreds,self.number=divmod(self.number,divider)
  22.         return self.hundredtext(hundreds)+['',thousandtext][hundreds>0]+['','-'][self.number>0 and hundreds>0]
  23.  
  24.     def hundredtext(self,hundreds):
  25.         hundred,tens=divmod(hundreds,100)
  26.         teen,single=divmod(tens,10)
  27.         return self.singles[hundred]+['','száz'][hundred>0]+ \
  28.                (self.teens[teen][single>0] if 0<teen<3 else self.teens[teen])+ \
  29.                self.singles[single]
  30.  
  31.  
  32. class NumToWordEng():
  33.     ''' Convert number to English words 1 to trillion-1'''    
  34.     def __init__(self,number):
  35.         self.number=int(number)
  36.         self.numbertext=''
  37.  
  38.         self.singles=['','one','two','three','four','five','six','seven','eight','nine']
  39.         self.tentonineteen=['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen', \
  40.                             'seventeen','eighteen','nineteen']
  41.         self.teens=['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
  42.  
  43.         for thousandtext,exp in [('billion',3),('million',2),('thousand',1),('',0)]:
  44.            self.numbertext+=self.thousandgroup(thousandtext,1000**exp)
  45.  
  46.     def thousandgroup(self,thousandtext,divider):
  47.         hundreds,self.number=divmod(self.number,divider)
  48.         return self.hundredtext(hundreds)+['',' '+thousandtext+' '][hundreds>0]
  49.            
  50.  
  51.     def hundredtext(self,hundreds):
  52.         hundred,tens=divmod(hundreds,100)
  53.         teen,single=divmod(tens,10)
  54.     ands=tens>0 and self.number<100 and (len(self.numbertext)>0 or hundred>0)
  55.         return self.singles[hundred]+['',' hundred '][hundred>0]+['','and '][ands]+ \
  56.                (self.tentonineteen[single] if teen==1 else self.teens[teen]+['','-'][single>0 and teen>1])+ \
  57.                ['',self.singles[single]][teen!=1]
  58.  
  59. if __name__=='__main__':
  60.     import sys
  61.     try:
  62.         number=int(sys.argv[1])
  63.         a=NumToWordHu(number)
  64.         b=NumToWordEng(number)
  65.         print(number)
  66.         print(a.numbertext)
  67.         print(b.numbertext)
  68.        
  69.     except:
  70.         print('Not number!, Nem szám!')
RAW Paste Data