Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Converting Int to Words
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Apr 2, 2024
1 parent a2aee4a commit 2786fe3
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 38 deletions.
62 changes: 62 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"cSpell.words": [
"billón",
"billones",
"catorce",
"CENTIS",
"ciento",
"cientos",
"cinco",
"cincuenta",
"convertir",
"cuarenta",
"cuatro",
"cuatrocientos",
"DECENAS",
"diecinueve",
"dieciocho",
"DIECIS",
"dieciséis",
"diecisiete",
"diez",
"doce",
"doscientos",
"empezar",
"entero",
"Entrada",
"estar",
"Ingrese",
"inválido",
"menor",
"millón",
"millones",
"mycompany",
"novecientos",
"noventa",
"nueve",
"número",
"Número",
"ochenta",
"ocho",
"ochocientos",
"palabra",
"palabras",
"puede",
"quinientos",
"Saliendo",
"salir",
"seis",
"seiscientos",
"sesenta",
"setecientos",
"setenta",
"siete",
"Tiene",
"trece",
"treinta",
"trescientos",
"UNIDADES",
"vacío",
"veinte"
]
}
140 changes: 102 additions & 38 deletions src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,30 @@ public class App {
void spawn_cli() {
// Enter data using BufferReader
var reader = new BufferedReader(new InputStreamReader(System.in));
String input;
while (true) {

// Reading data using readLine
String name = new String("asdasd");
try {
System.out.println("Ingrese info");
name = reader.readLine();
System.out.println("FIn");
} catch (Exception e) {
System.out.println("Errir");
System.out.println(e);
}


System.out.println("FIn2");
// Printing the read line
System.out.println(name);
// let mut input = String::new();
// print!("\nIngrese un número para convertir a palabras\nIngrese `exit` para
// salir:\n\n");
// fn read_line(input: &mut String) {
// input.clear();
// std::io::stdin().read_line(input).unwrap();
// }
// Reading data using readLine
try {
System.out.println("Ingrese numero o `exit` para salir:");
input = reader.readLine().trim();
} catch (Exception e) {
System.out.println("Entrada invalida: " + e);
continue;
}
if (input.equals("exit")) {
System.out.println("Saliendo...");
break;
}
if (input.equals("clear")) {
System.out.println("\033[H\033[2J");
continue;
}
}

// loop {
// print!("Ingrese su número: ");
// flush();
// read_line(&mut input);
// let input = input.trim();

// match input {
// "exit" => {
// clear_terminal();
Expand Down Expand Up @@ -87,29 +82,98 @@ public static void main(String[] args) {
App app = new App();

app.spawn_cli();
System.out.println("Goodbye World!");
var palabra = Int2Words.to_cardinal(1_001_020_642);
System.out.println("Goodbye World!: " + palabra);
System.out.println("=" + Int2Words.to_cardinal(2_001_020_642));
System.out.println("=" + Int2Words.to_cardinal(2_002_020_642));
System.out.println("=" + Int2Words.to_cardinal(2_020_021_642));
System.out.println("=" + Int2Words.to_cardinal(2_000_020_642));
}
}

class Int2Words {

// let mut vec = vec![];
// while num > 0 {
// vec.push((num % 1000) as i16);
// num /= 1000;
// }
// vec.reverse();
// let prettied =
// vec.into_iter().map(|num|
// format!("{num:03}")).collect::<Vec<String>>().join(",");
static String to_cardinal(Integer num) {
if (num == 0) {
return "cero";
}
var triplets = Utils.into_triplets(num);

var words = new ArrayList<String>();
for (int i = triplets.length - 1; i >= 0; i--) {
// Read the triplets in an inverted order
var triplet = triplets[triplets.length - i - 1];
if (triplet == 0) {
continue;
}
int hundreds = (triplet / 100) % 10;
int tens = (triplet / 10) % 10;
int units = triplet % 10;
if (hundreds > 0) {
if (triplet == 100) {
// Edge case when triplet is a hundred
words.add("cien");
} else {
words.add(CENTIS[hundreds]);
}
}

// print!("{:?}", prettied.trim_start_matches('0'));
// flush();
if (tens != 0 || units != 0) {
// for edge case when unit value is 1 and is not the last triplet
String unit_word = null;
if (units == 1 && i != 0) {
unit_word = "un";
} else {
unit_word = UNIDADES[units];
}
if (tens == 0) {
// case `?_102` => `? ciento dos`
words.add(unit_word);
} else if (tens == 1) {
// case `?_119` => `? ciento diecinueve`
// case `?_110` => `? ciento diez`
words.add(DIECIS[units]);
} else {
// case `?_142 => `? cuarenta y dos`
String ten = DECENAS[tens];
if (units == 0) {
words.add(ten);
} else {
words.add(String.format("%s y %s", ten, unit_word));
}
}
if (i != 0 && triplet != 0) {
if (i > MILES.length - 1) {
return String.format("Número demasiado grande: %d - Maximo: %d", num, Integer.MAX_VALUE);
}
// Boolean that checks if next MILES is plural
boolean plural = triplet != 1;
if (plural) {
words.add(MILES[i]);
} else {
words.add(MIL[i]);
}
}

}
}
return String.join(" ", words);
}

static final String[] UNIDADES = { "", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve" };
static final String[] DIECIS = { "diez", "once", "doce", "trece", "catorce", "quince", "dieciséis", "diecisiete",
"dieciocho", "diecinueve" };
static final String[] DECENAS = { "", "", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta",
"ochenta", "noventa" };
static final String[] CENTIS = { "", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos",
"seiscientos",
"setecientos", "ochocientos", "novecientos" };
static final String[] MILES = { "", "mil", "millones", "billones" };
static final String[] MIL = { "", "mil", "millón", "billón" };
}

class Utils {
static Integer[] into_triplets(Integer num) {
static public Integer[] into_triplets(Integer num) {

var vec = new ArrayList<Integer>();
while (num > 0) {
Expand Down

0 comments on commit 2786fe3

Please sign in to comment.