-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonedaLetra.cs
188 lines (155 loc) · 5.62 KB
/
MonedaLetra.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumLetras
{
public class MonedaLetra
{
double _numero;
int _entero;
int _centavos;
string _nom_moneda;
string _nom_centimos;
public double Moneda { get { return _numero; } }
public MonedaLetra(double numero, string moneda, string centimos)
{
_numero = Math.Round(numero, 2);
_entero = (int)Math.Floor(_numero);
_centavos = (int)(Math.Round(_numero - _entero, 2) * 100.0D);
_nom_moneda = moneda;
_nom_centimos = centimos;
}
public override string ToString()
{
string num = string.Empty;
string cents = string.Empty;
cents = _centavos > 0 ? " CON " + Letras0_99(_centavos) + _nom_centimos + (_centavos > 1 ? "S" : "") : string.Empty;
num = string.Format("{0}{1}{2}{3}", Letras(_entero), _nom_moneda, _numero > 1 ? "S" : string.Empty, cents);
return num;
}
public string Letras0_99(int numero)
{
string letras = string.Empty;
Dictionary<int, string> unidades = new Dictionary<int, string>()
{
{ 0 , ""},
{ 1 , "UN "},
{ 2 , "DOS "},
{ 3 , "TRES "},
{ 4 , "CUATRO "},
{ 5 , "CINCO "},
{ 6 , "SEIS "},
{ 7 , "SIETE "},
{ 8 , "OCHO "},
{ 9 , "NUEVE "},
{ 10 , "DIEZ "},
{ 11 , "ONCE "},
{ 12 , "DOCE "},
{ 13 , "TRECE "},
{ 14 , "CATORCE "},
{ 15 , "QUINCE "},
{ 20 , "VEINTE "},
//{ 21 , "VEINTIÚN "},
{ 30 , "TREINTA "},
//{ 31 , "TREINTAIÚN "},
{ 40 , "CUARENTA "},
//{ 41 , "CUARENTAIÚN "},
{ 50 , "CINCUENTA "},
//{ 51 , "CINCUENTAIÚN "},
{ 60 , "SESENTA "},
//{ 61 , "SESENTAIÚN "},
{ 70 , "SETENTA "},
//{ 71 , "SETENTAIÚN "},
{ 80 , "OCHENTA "},
//{ 81 , "OCHENTAIÚN "},
{ 90 , "NOVENTA "},
//{ 91 , "NOVENTAIÚN "},
};
Dictionary<int, string> decenas = new Dictionary<int, string>()
{
{0,""},
{ 10 , "DIECI"},
{ 20 , "VEINTI"},
{ 30 , "TREINTA Y "},
{ 40 , "CUARENTA Y "},
{ 50 , "CINCUENTA Y "},
{ 60 , "SESENTA Y "},
{ 70 , "SETENTA Y "},
{ 80 , "OCHENTA Y "},
{ 90 , "NOVENTA Y "},
};
if (unidades.ContainsKey(numero))
{
letras = unidades[numero];
}
else
{
int parteUnidad = numero % 10;
int parteDecena = (numero % 100) - parteUnidad;
string letrasUnidad = unidades[parteUnidad];
string letrasDecena = decenas[parteDecena];
letras = letrasDecena + letrasUnidad;
}
return letras;
}
public string Letras0_999(int numero)
{
string letras = string.Empty;
Dictionary<int, string> especial = new Dictionary<int, string>()
{
{0,"CERO "},
{100,"CIEN "},
{101,"CIENTO UNO "},
};
Dictionary<int, string> centenas = new Dictionary<int, string>()
{
{0,""},
{ 100 , "CIENTO "},
{ 200 , "DOS CIENTOS "},
{ 300 , "TRES CIENTOS "},
{ 400 , "CUATRO CIENTOS "},
{ 500 , "QUINIENTOS "},
{ 600 , "SEIS CIENTOS "},
{ 700 , "SETECIENTOS "},
{ 800 , "OCHO CIENTOS "},
{ 900 , "NOVECIENTOS "},
};
if (especial.ContainsKey(numero))
{
letras = especial[numero];
}
else
{
int parteUnidad = numero % 10;
int parteDecena = (numero % 100) - parteUnidad;
int parteCentena = (numero % 1000) - parteDecena - parteUnidad;
string letras0_99 = Letras0_99(parteUnidad + parteDecena);
string letrasCentenas = centenas[parteCentena];
letras = letrasCentenas + letras0_99;
}
return letras;
}
public string Letras(int numero)
{
string parte = string.Empty;
string letras = string.Empty;
string cantidad = new string(numero.ToString().Reverse().ToArray());
int n = (int)Math.Ceiling((double)cantidad.Length/3.0D);
int digitos = 0;
int tam = cantidad.Length;
string mil;
for (int i = 0; i < n; i++)
{
tam = cantidad.Length - i * 3;
digitos = tam > 3 ? 3 : tam;
parte = cantidad.Substring(i * 3, digitos);
parte = new string(parte.Reverse().ToArray());
digitos = int.Parse(parte);
mil = i == 1 ? "MIL " : string.Empty;
letras = digitos == 1 && i == 1 ? "MIL " : Letras0_999(digitos) + mil + letras;
}
return letras;
}
}
}