Advertisement
Guest User

Slovní vyjádření částky

a guest
Sep 23rd, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1.         public static string CisloSlovy(this long cislo)
  2.         {
  3.             string result = string.Empty;
  4.             if (cislo < 0)
  5.                 result = "mínus ";
  6.             string numero = Math.Abs(cislo).ToString();
  7.             if (numero.Length < 13)
  8.             {
  9.                 //slovníky pro číslice na jednotlivých pozicích převáděného čísla
  10.                 Array[] slovnik = new Array[12];
  11.                 slovnik[0] = new[] { "", "jedna", "dvě", "tři", "čtyři", "pět", "šest", "sedm", "osm", "devět" };
  12.                 slovnik[1] = new[] { "", "", "dvacet", "třicet", "čtyřicet", "padesát", "šedesát", "sedmdesát", "osmdesát", "devadesát" };
  13.                 slovnik[2] = new[] { "", "sto", "dvě stě", "tři sta", "čtyři sta", "pět set", "šest set", "sedm set", "osm set", "devět set" };
  14.                 slovnik[3] = new[] { "tisíc", "jeden tisíc", "dva tisíce", "tři tisíce", "čtyři tisíce", "pět tisíc", "šest tisíc", "sedm tisíc", "osm tisíc", "devět tisíc" };
  15.                 slovnik[4] = new[] { "", "deset", "dvacet", "třicet", "čtyřicet", "padesát", "šedesát", "sedmdesát", "osmdesát", "devadesát" };
  16.                 slovnik[5] = new[] { "", "sto", "dvě stě", "tři sta", "čtyři sta", "pět set", "šest set", "sedm set", "osm set", "devět set" };
  17.                 slovnik[6] = new[] { "milionů", "jeden milion", "dva miliony", "tři miliony", "čtyři miliony", "pět milionů", "šest milionů", "sedm milionů", "osm milionů", "devět milionů" };
  18.                 slovnik[7] = new[] { "", "deset", "dvacet", "třicet", "čtyřicet", "padesát", "šedesát", "sedmdesát", "osmdesát", "devadesát" };
  19.                 slovnik[8] = new[] { "", "sto", "dvě stě", "tři sta", "čtyři sta", "pět set", "šest set", "sedm set", "osm set", "devět set" };
  20.                 slovnik[9] = new[] { "miliard", "jedna miliarda", "dvě miliardy", "tři miliardy", "čtyři miliardy", "pět miliard", "šest miliard", "sedm miliard", "osm miliard", "devět miliard" };
  21.                 slovnik[10] = new[] { "", "deset", "dvacet", "třicet", "čtyřicet", "padesát", "šedesát", "sedmdesát", "osmdesát", "devadesát" };
  22.                 slovnik[11] = new[] { "", "sto", "dvě stě", "tři sta", "čtyři sta", "pět set", "šest set", "sedm set", "osm set", "devět set" };
  23.  
  24.                 var slovnik1019 = new[] { "deset", "jedenáct", "dvanáct", "třináct", "čtrnáct", "patnáct", "šestnáct", "sedmnáct", "osmnáct", "devatenáct" };
  25.  
  26.                 for (var x = 0; x <= numero.Length - 1; x++)
  27.                 {
  28.                     //vypsání čísel obsahujících 10-19 ze slovníku 10-19
  29.                     if (((x == numero.Length - 2) || (x == numero.Length - 5) || (x == numero.Length - 8) || (x == numero.Length - 11)) && (numero[x] == '1'))
  30.                     {
  31.                         result += string.Format("{0} ", slovnik1019[int.Parse(numero[x + 1].ToString())]);
  32.                         var arr = slovnik[numero.Length - 2 - x];
  33.                         string value = (string)arr.GetValue(0);
  34.                         if (!string.IsNullOrEmpty(value))
  35.                             result += string.Format("{0} ", value);
  36.                         x++;
  37.                         continue;
  38.                     }
  39.                     //přeskočení skupin číslic 000 kvůli eliminaci duplicit milion tisíc, miliarda milion, ...
  40.                     if (numero.Length > 6 && (x == numero.Length - 4 || x == numero.Length - 7) && x - 2 >= 0 && numero.Substring(x - 2, 3).Equals("000"))
  41.                     {
  42.                         continue;
  43.                     }
  44.                     else
  45.                     {
  46.                         var arr = slovnik[numero.Length - 1 - x];
  47.                         string value = (string)arr.GetValue(int.Parse(numero[x].ToString()));
  48.                         if (!string.IsNullOrEmpty(value))
  49.                             result += string.Format("{0} ", value);
  50.                     }
  51.                 }
  52.             }
  53.             else
  54.                 result = numero;
  55.  
  56.             return result.TrimEnd();
  57.         }
  58.  
  59.         public static string CastkaSlovy(this decimal castka)
  60.         {
  61.             if (castka > long.MaxValue)
  62.                 return castka.ToString();
  63.             else
  64.             {
  65.                 castka = Zaokrouhlovani.Round(castka, 2);
  66.                 long celaCastka = (long) decimal.Truncate(castka);
  67.                 long zbytek = (long) ((Math.Abs(castka) - Math.Abs(decimal.Truncate(castka))) * 100);
  68.                 string slovy = celaCastka.CisloSlovy();
  69.                 if (slovy.Length > 0)
  70.                     slovy = string.Format("{0} {1}", slovy, celaCastka.KorunSlovy());
  71.                 if (zbytek != decimal.Zero)
  72.                     slovy = string.Format("{0}{1}{2} haléřů", slovy, string.Empty.PadLeft(slovy.Length > 0 ? 1 : 0), zbytek.CisloSlovy());
  73.  
  74.                 return slovy;
  75.             }
  76.         }
  77.  
  78.         public static string KorunSlovy(this long kc)
  79.         {
  80.             var slovnik = new[] { "korun", "koruna", "koruny", "koruny", "koruny" };
  81.  
  82.             if (slovnik.Length - 1 >= Math.Abs(kc))
  83.                 return slovnik[Math.Abs(kc)];
  84.             else
  85.             {
  86.                 if (kc.ToString().EndsWith("2"))
  87.                     return slovnik[2]; //např. padesát dvě koruny - výjimka; ostatní skloňujeme xx korun
  88.                 else
  89.                     return slovnik[0];
  90.             }
  91.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement