home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 31 / FreelogHS31.iso / ArgentCompta / Bankperfect / bp.exe / Scripts / Numerals / numerals.py < prev   
Text File  |  2005-09-20  |  4KB  |  126 lines

  1. import BP
  2.  
  3. def DecToRoman(Value):
  4.   vals = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]
  5.   try:
  6.     i = long(Value)
  7.     if i < 1 or i > 3999: return ""
  8.     Result = ""
  9.     for d, r in vals:
  10.       while i >= d:
  11.         Result += r
  12.         i -= d
  13.     return Result
  14.   except:
  15.     return ""
  16.  
  17. def RomanToDec(Roman):
  18.   try:
  19.     Dic = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
  20.     Roman = Roman[::-1].upper()
  21.     Result = 0
  22.     h = Dic[Roman[0]]
  23.     for c in Roman:
  24.       Current = Dic[c]
  25.       if Current >= h:
  26.         Result += Current
  27.         h = Current
  28.       else: Result -= Current
  29.     if Result > 3999 or Result < 1: return ""
  30.     return str(Result)
  31.   except:
  32.     return ""
  33.  
  34. def DecToText(Value):
  35.   uni = ['un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize']
  36.   diz = ['vingt', 'trente', 'quarante', 'cinquante', 'soixante', '', 'quatre-vingt']
  37.   mil = ['cent', 'mille', 'million', 'milliard', 'billion', 'milliers-de-billions', 'trillion']
  38.   try:
  39.     N = long(Value)
  40.     if N < 0: return ""
  41.   except:
  42.     return ""
  43.  
  44.   if N == 0: return 'ZΘro'
  45.   Result = ""
  46.   k = 0
  47.   
  48.   while N != 0:
  49.     U = N % 10
  50.     N = N / 10
  51.     D = N % 10
  52.     N = N / 10
  53.     if D in [1, 7, 9]:
  54.       D -= 1
  55.       U += 10
  56.     s = ''
  57.     if D > 1:
  58.       s = ' ' + diz[D - 2]
  59.       if (D < 8) and ((U == 1) or (U == 11)): s += ' et'
  60.     if U > 16:
  61.       s += ' ' + uni[9]
  62.       U -= 10
  63.     if U > 0: s += ' ' + uni[U - 1]
  64.     if (Result == '') and (D == 8) and (U == 0): Result = 's'
  65.     Result = s + Result
  66.  
  67.     C, N = N % 10, N / 10
  68.     if C > 0:
  69.       s = ''
  70.       if C > 1: s = ' ' + uni[C - 1] + s
  71.       s += ' ' + mil[0]
  72.       if (Result == '') and (C > 1): Result = 's'
  73.       Result = s + Result
  74.  
  75.     if N > 0:
  76.       k += 1
  77.       I = N % 1000
  78.       if (I > 1) and (k > 1): Result = 's' + Result
  79.       if I > 0: Result = ' ' + mil[k] + Result
  80.       if (I == 1) and (k == 1): N -= 1
  81.  
  82.   return Result.replace('un milliers', 'un millier').replace('llionss', 'llions').strip()
  83.  
  84. def CloseForm(Sender):
  85.   f.Close()
  86.  
  87. def FChange(Sender):
  88.   if Sender.Name == "EditDec":
  89.     EditRom.OnChange = None
  90.     EditRom.Text = DecToRoman(EditDec.Text)
  91.     EditRom.OnChange = FChange
  92.   elif Sender.Name == "EditRom":
  93.     EditDec.OnChange = None
  94.     EditDec.Text = RomanToDec(EditRom.Text)
  95.     EditDec.OnChange = FChange
  96.   EditTxt.Lines.Text = DecToText(EditDec.Text)
  97.  
  98. f = CreateComponent("TForm", None)
  99. f.SetProps(Position = "poScreenCenter", Width = 400, Height = 300, Caption = "Conversions numΘriques")
  100. f.Font.Name = "Tahoma"
  101. f.Constraints.MinWidth = 400
  102. f.Constraints.MinHeight = 300
  103.  
  104. LabelDec = CreateComponent("TLabel", f)
  105. LabelDec.SetProps(Left=20, Top=20, Caption="Chiffres :", Parent=f)
  106. EditDec = CreateComponent("TEdit", f)
  107. EditDec.SetProps(Name="EditDec", Anchors=["akLeft", "akRight", "akTop"], MaxLength=21, Left=20, Top=35, Width=350, Parent=f)
  108. EditDec.Text = ""
  109.  
  110. LabelRom = CreateComponent("TLabel", f)
  111. LabelRom.SetProps(Left=20, Top=70, Caption="Romains :", Parent=f)
  112. EditRom = CreateComponent("TEdit", f)
  113. EditRom.SetProps(Name="EditRom", CharCase="ecUpperCase", Anchors=["akLeft", "akRight", "akTop"], Left=20, Top=85, Width=350, Parent=f)
  114. EditRom.Text = ""
  115.  
  116. LabelTxt = CreateComponent("TLabel", f)
  117. LabelTxt.SetProps(Left=20, Top=120, Caption="Lettres :", Parent=f)
  118. EditTxt = CreateComponent("TMemo", f)
  119. EditTxt.SetProps(Left=20, Top=135, Width=350, Height=110, ReadOnly=1, Anchors=["akLeft", "akRight", "akTop", "akBottom"], Parent=f)
  120. EditDec.OnChange = FChange
  121. EditRom.OnChange = FChange
  122.  
  123. ButtonClose = CreateComponent("TButton", f)
  124. ButtonClose.SetProps(Left=150, Top=260, Width=100, Height=25, Default=1, Cancel=1, Caption="Fermer", Anchors=["akBottom"], OnClick=CloseForm, Parent=f)
  125.  
  126. f.ShowModal()