Código de Java - Numero a Letras

sin imagen de perfil
Val: 22
Ha aumentado 1 puesto en Java (en relación al último mes)
Gráfica de Java

Numero a Letrasgráfica de visualizaciones


Java

Publicado el 7 de Agosto del 2018 por Nelson (4 códigos)
3.438 visualizaciones desde el 7 de Agosto del 2018
desarrollo de código para convertir números a monedas, aun falta terminarlo pero esta en las 5 cifras perfectamente.

Requerimientos

aqui dejo el link de github para quien desee seguir y ver al momento q este listo
https://github.com/neonemesis5/Numeros-a-Letras/blob/master/NumToLetra.java

1

Publicado el 7 de Agosto del 2018gráfica de visualizaciones de la versión: 1
3.439 visualizaciones desde el 7 de Agosto del 2018
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.io.PrintWriter;
import java.util.ArrayList;
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Nelson E
 */
public class NumToLetra {
 
    private ArrayList<Numero> Numeros = new ArrayList<Numero>() {
        {
//            add(new Numero(0, "CERO"));
            add(new Numero(1, "UN"));
            add(new Numero(1, "UNO"));
            add(new Numero(2, "DOS"));
            add(new Numero(3, "TRES"));
            add(new Numero(4, "CUATRO"));
            add(new Numero(5, "CINCO"));
            add(new Numero(6, "SEIS"));
            add(new Numero(7, "SIETE"));
            add(new Numero(8, "OCHO"));
            add(new Numero(9, "NUEVE"));
            add(new Numero(10, "DIEZ"));
            add(new Numero(11, "ONCE"));
            add(new Numero(12, "DOCE"));
            add(new Numero(13, "TRECE"));
            add(new Numero(14, "CATORCE"));
            add(new Numero(15, "QUINCE"));
            add(new Numero(16, "DIECISEIS"));
            add(new Numero(17, "DIECISIETE"));
            add(new Numero(18, "DIECIOCHO"));
            add(new Numero(19, "DIECINUEVE"));
            add(new Numero(20, "VEINTE"));
            add(new Numero(30, "TREINTA"));
            add(new Numero(40, "CUARENTA"));
            add(new Numero(50, "CINCUENTA"));
            add(new Numero(60, "SESENTA"));
            add(new Numero(70, "SETENTA"));
            add(new Numero(80, "OCHENTA"));
            add(new Numero(90, "NOVENTA"));
            add(new Numero(100, "CIEN"));
        }
    };
 
    private String MilTex[] = new String[]{"MIL", "MILLONES"};
 
    private String getNum(String Num) {
        String word = "";
        switch (Num.length()) {
            case 1://<10
                return Numeros.get(toInt(Num)).getLetra();
            case 2://<100
                if (Num.matches("[1][0-9]")) {
                    return Numeros.get(toInt(Num)).getLetra();
                }
                if (Num.matches("[2-9][0]")) {
                    for (int i = 20; i < Numeros.size(); i++) {
                        if (Numeros.get(i).isEquals(toInt(Num))) {
                            return Numeros.get(i).getLetra();
                        }
                    }
                }
                //tomando el mas significativo
                int Ms = toInt(String.valueOf(Num.charAt(0)));
                Ms *= 10;
                return getNum(String.valueOf(Ms)) + " Y " + getNum(String.valueOf(Num.charAt(1)));
            case 3://cientos centenas <1000
                if (Num.matches("[1][0][0]")) {//primero 100
                    return "CIEN";
                }
                if (Num.matches("[1][0][1-9]")) {//101-109
                    return "CIENTO " + getNum(Num.substring(2, Num.length()));
                }
                if (Num.matches("[1][1-9][0]")) {//110,120,130,190
                    return "CIENTO " + getNum(Num.substring(1, Num.length()));
                }
                if (Num.matches("[1][1][1-9]")) {//111-119
                    return "CIENTO " + getNum(Num.substring(1, Num.length()));
                }
                if (Num.matches("[1][2-9][1-9]")) {//121-199
                    return "CIENTO " + getNum(Num.substring(1, Num.length()));
                }
                if (Num.matches("[2-9][0][0]")) {//enteros 200,300,400,etc...900
                    return getNum(String.valueOf(Num.charAt(0))) + "CIENTOS";
                }
                if (Num.matches("[2-9][0][1-9]")) {//201-209
                    return getNum(String.valueOf(Num.charAt(0))) + "CIENTOS " + getNum(Num.substring(2, Num.length()));
                }
                if (Num.matches("[2-9][1][0-9]")) {//210-219
                    return getNum(String.valueOf(Num.charAt(0))) + "CIENTOS " + getNum(Num.substring(1, Num.length()));
                }
                if (Num.matches("[2-9][2-9][0-9]")) {//221-999
                    return getNum(String.valueOf(Num.charAt(0))) + "CIENTOS " + getNum(Num.substring(1, Num.length()));
                }
            case 4://miles <10.000
                if (Num.matches("[1][0][0][0]")) {//primero 1000
                    return "MIL";
                }
                if (Num.matches("[1][0][0][1-9]")) {//1001-1009
                    return "MIL " + getNum(String.valueOf(Num.charAt(3)));
                }
                if (Num.matches("[1][0][0-9][0-9]")) {//1010-1019
                    return "MIL " + getNum(Num.substring(2));
                }
                if (Num.matches("[1][0-9][0-9][0-9]")) {//1001-1999
                    return "MIL " + getNum(Num.substring(1));
                }
                if (Num.matches("[2-9][0][0][0]")) {//2000,3000,4000...9000
                    return getNum(String.valueOf(Num.charAt(0))) + " MIL ";
                }
                if (Num.matches("[2-9][1-9][0-9][0-9]")) {//2100,3900,4000...9900
                    return getNum(String.valueOf(Num.charAt(0))) + " MIL " + getNum(Num.substring(1));
                }
                if (Num.matches("[2-9][0][1-9][0-9]")) {//2010-2099
                    return getNum(String.valueOf(Num.charAt(0))) + " MIL " + getNum(Num.substring(2));
                }
                if (Num.matches("[2-9][0][0][1-9]")) {//2001 -2009
                    return getNum(String.valueOf(Num.charAt(0))) + " MIL " + getNum(String.valueOf(Num.charAt(3)));
                }
 
            case 5://<100000
                if (Num.matches("[1-9][0-9][0][0][0]")) {//10mil,20mil,...90mil
                    return getNum(Num.substring(0, 2)) + " MIL";
                }
                if (Num.matches("[1-9][0-9][0][0][1-9]")) {//10mil,20mil,...90mil
                    return getNum(Num.substring(0, 2)) + " MIL " + getNum(Num.substring(4, 5));
                }
                if (Num.matches("[1-9][0-9][0][0-9][0-9]")) {//
                    return getNum(Num.substring(0, 2)) + " MIL " + getNum(Num.substring(3, 5));
                }
                if (Num.matches("[1-9][0-9][0-9][0-9][0-9]")) {//
                    return getNum(Num.substring(0, 2)) + " MIL " + getNum(Num.substring(2, 5));
                }
            case 6://<100000
                if (Num.matches("[1-9][0][0][0][0][0]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL";
                }
                if (Num.matches("[1-9][0-9][0-9][0][0][0]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL";
                }
                if (Num.matches("[1-9][0-9][0-9][0][0][1-9]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL "+getNum(Num.substring(5, 6));
                }
                if (Num.matches("[1-9][0-9][0-9][0][1-9][0]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL "+getNum(Num.substring(4, 6));
                }
                if (Num.matches("[1-9][0-9][0-9][0-9][1-9][0]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL "+getNum(Num.substring(3, 6));
                }
                if (Num.matches("[1-9][0-9][0-9][0-9][0][0]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL "+getNum(Num.substring(3, 6));
                }
 
                if (Num.matches("[1-9][0][0][0][0-9][0-9]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL " + getNum(Num.substring(4, 6));
                }
                if (Num.matches("[1-9][0][0][0-9][0-9][0-9]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL " + getNum(Num.substring(3, 6));
                }
                if (Num.matches("[1-9][0][0-9][0][1-9][1-9]")) {//
                    return getNum(Num.substring(0, 3)) + " MIL " + getNum(Num.substring(4, 6));
                }
 
        }
        return word;
    }
 
    public static void main(String[] args) {
        try {
            PrintWriter pf = new PrintWriter("d:\\salida.txt");
            NumToLetra num = new NumToLetra();
            for (int i = 101100; i < 200000; i++) {
                //System.out.println(i + " - " + num.Letras(String.valueOf(i)));
                pf.append(i + " - " + num.Letras(String.valueOf(i)) + "\n");
 
            }
            pf.close();
        } catch (Exception e) {
 
        }
    }
 
    public String Letras(String value) {
        String aux = getNum(value);
        int pos = aux.indexOf("VEINTE Y ");
        if (pos != -1) {
            aux = aux.replace("VEINTE Y ", "VEINTI");
        }
        pos = aux.indexOf("CINCOCIENTOS ");
        if (pos != -1) {
            aux = aux.replace("CINCOCIENTOS ", "QUINIENTOS ");
        }
        pos = aux.indexOf("CINCOCIENTOS");
        if (pos != -1) {
            aux = aux.replace("CINCOCIENTOS", "QUINIENTOS");
        }
        pos = aux.indexOf("SIETECIENTOS ");
        if (pos != -1) {
            aux = aux.replace("SIETECIENTOS ", "SETECIENTOS ");
        }
        pos = aux.indexOf("SIETECIENTOS");
        if (pos != -1) {
            aux = aux.replace("SIETECIENTOS", "SETECIENTOS");
        }
        pos = aux.indexOf("NUEVECIENTOS ");
        if (pos != -1) {
            aux = aux.replace("NUEVECIENTOS ", "NOVECIENTOS ");
        }
        pos = aux.indexOf("NUEVECIENTOS");
        if (pos != -1) {
            aux = aux.replace("NUEVECIENTOS", "NOVECIENTOS");
        }
        return aux;
    }
 
    private int toInt(String v) {
        if (v != null) {//q no sea null
            if (v.length() != 0) {//que no este vacia
                if (v.matches("\\d+")) {
                    return Integer.parseInt(v);
                }
            }
        }
        return -1;
    }
}
 
class Numero {
 
    private int numero;
    private String Letras;
 
    public Numero(int numero, String Letra) {
        this.numero = numero;
        this.Letras = Letra;
    }
 
    public int getNumero() {
        return numero;
    }
 
    public String getLetra() {
        return Letras;
    }
 
    public boolean isEquals(int value) {
        return value == numero;
    }
}



Comentarios sobre la versión: 1 (0)


No hay comentarios
 

Comentar la versión: 1

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s4742