1: using System;
2: using System.Collections;
3: using System.Text;
4:
5: namespace Teste
6: {
7:
8: public class NumeroPorExtenso
9: {
10:
11: private ArrayList numeroLista;
12:
13: private Int32 num;
14:
15: //array de 2 linhas e 14 colunas[2][14]
16: private static readonly String[,] qualificadores = new String[,] {
17: // {"milésimo de real","milésimos de real"},//[0][0] e [0][1]
18: {"centavo", "centavos"},//[1][0] e [1][1]
19: {"", ""},//[2][0],[2][1]
20: {"mil", "mil"},
21: {"milhão", "milhões"},
22: {"bilhão", "bilhões"},
23: {"trilhão", "trilhões"},
24: {"quatrilhão", "quatrilhões"},
25: {"quintilhão", "quintilhões"},
26: {"sextilhão", "sextilhões"},
27: {"setilhão", "setilhões"},
28: {"octilhão","octilhões"},
29: {"nonilhão","nonilhões"},
30: {"decilhão","decilhões"}
31: };
32:
33: private static readonly String[,] numeros = new String[,] {
34: {"zero", "um", "dois", "três", "quatro",
35: "cinco", "seis", "sete", "oito", "nove",
36: "dez","onze", "doze", "treze", "quatorze",
37: "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"},
38: {"vinte", "trinta", "quarenta", "cinqüenta", "sessenta",
39: "setenta", "oitenta", "noventa",null,null,null,null,null,null,null,null,null,null,null,null},
40: {"cem", "cento",
41: "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos",
42: "setecentos", "oitocentos", "novecentos",null,null,null,null,null,null,null,null,null,null}
43: };
44:
45: public NumeroPorExtenso() {
46: numeroLista = new ArrayList();
47: }
48:
49: public NumeroPorExtenso(Decimal dec) {
50: numeroLista = new ArrayList();
51: SetNumero(dec);
52: }
53:
54: public void SetNumero(Decimal dec) {
55: dec = Decimal.Round(dec,2);
56: dec = dec * 100;
57: num = Convert.ToInt32(dec);
58:
59: numeroLista.Clear();
60: if (num == 0) {
61: numeroLista.Add(0);
62: numeroLista.Add(0);
63: } else {
64: AddRemainder(100);
65:
66: while (num != 0) {
67: AddRemainder(1000);
68: }
69:
70: }
71:
72: }
73:
74: private void AddRemainder(Int32 divisor) {
75: Int32 div = num / divisor;
76: Int32 mod = num % divisor;
77:
78: Int32[] newNum = new Int32[] {div,mod};
79:
80: numeroLista.Add(mod);
81:
82: num = div;
83: }
84:
85: private bool TemMaisGrupos(Int32 ps) {
86: while (ps > 0) {
87: if ((Int32) numeroLista[ps] != 00 && !TemMaisGrupos(ps -1))
88: return true;
89: ps--;
90: }
91: return true;
92: }
93:
94: private bool EhPrimeiroGrupoUm() {
95: if ((Int32) numeroLista[numeroLista.Count-1] == 1)
96: return true;
97: else
98: return false;
99: }
100:
101: private bool EhUltimoGrupo(Int32 ps) {
102: return((ps > 0) && ((Int32) numeroLista[ps] != 0) || !TemMaisGrupos(ps - 1));
103: }
104:
105: private bool EhGrupoZero(Int32 ps) {
106: if (ps <= 0 || ps >= numeroLista.Count)
107: return true;
108: return ((Int32) numeroLista[ps] == 0);
109: }
110:
111: private bool EhUnicoGrupo() {
112: if (numeroLista.Count <= 3)
113: return false;
114:
115: if (!EhGrupoZero(1) && !EhGrupoZero(2))
116: return false;
117:
118: bool hasOne = false;
119:
120: for (Int32 i=3; i < numeroLista.Count; i++) {
121: if ((Int32) numeroLista[i] != 0) {
122: if (hasOne)
123: return false;
124: hasOne = true;
125: }
126: }
127: return true;
128: }
129:
130: private String NumToString(Int32 numero,Int32 escala) {
131: Int32 unidade = (numero % 10);
132: Int32 dezena = (numero % 100);
133: Int32 centena = (numero / 100);
134:
135: StringBuilder buf = new StringBuilder();
136:
137: if (numero != 0) {
138: if (centena != 0) {
139: if (dezena == 0 && centena == 1) {
140: buf.Append(numeros[2,0]);
141: } else {
142: buf.Append(numeros[2,centena]);
143: }
144: }
145:
146: if (buf.Length > 0 && dezena != 0) {
147: buf.Append(" e ");
148: }
149:
150: if (dezena > 19) {
151: dezena = dezena / 10;
152: buf.Append(numeros[1,dezena-2]);
153: if (unidade != 0) {
154: buf.Append(" e ");
155: buf.Append(numeros[0,unidade]);
156: }
157: } else if (centena == 0 || dezena != 0) {
158: buf.Append(numeros[0,dezena]);
159: }
160:
161: buf.Append(" ");
162:
163: if (numero == 1) {
164: buf.Append(qualificadores[escala,0]);
165: } else {
166: buf.Append(qualificadores[escala,1]);
167: }
168:
169: }
170: return buf.ToString();
171: }
172:
173: public String ToString() {
174: StringBuilder buf = new StringBuilder();
175:
176: Int32 numero = (Int32) numeroLista[0];
177: Int32 count;
178: for (count = numeroLista.Count -1; count > 0; count--) {
179: if (buf.Length > 0 && !EhGrupoZero(count)) {
180: buf.Append(" e ");
181: }
182: buf.Append(NumToString((Int32) numeroLista[count],count));
183: }
184:
185: if (buf.Length > 0) {
186:
187: while (buf.ToString().EndsWith(" "))
188: buf.Length = buf.Length -1;
189:
190: if (EhUnicoGrupo()) {
191: buf.Append(" de ");
192: }
193:
194: if (EhPrimeiroGrupoUm()) {
195: buf.Insert(0,"h");
196: }
197:
198: if (numeroLista.Count == 2 && ((Int32) numeroLista[1] == 1)) {
199: buf.Append(" real");
200: } else {
201: buf.Append(" reais");
202: }
203:
204: if ((Int32) numeroLista[0] != 0) {
205: buf.Append(" e ");
206: }
207: }
208:
209: if ((Int32) numeroLista[0] != 0) {
210: buf.Append(NumToString((Int32) numeroLista[0],0));
211: }
212:
213: return buf.ToString();
214: }
215: }
216: }