HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Number to spanish word converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wordconverternumberspanish

Problem

This converts from a number to a Spanish word for example :

100 => cien

200 => doscientos


I used a divide-and-conquer approach:

Constants:

```
package numberTranslator;

import java.util.Arrays;
import java.util.Comparator;

public enum ExSpanishSufix {

ciento("s",2),
mil("",3),
millon("es",6),
billon("es",9),
trillon("es",12),
quadrillion("es",15);

private String plural;
private int exponent;
private long value;

private ExSpanishSufix(String plural, int exponent){

this.plural= plural;
this.exponent=exponent;
this.value=(long)Math.pow(10, exponent);

}

public static ExSpanishSufix[] getSorted(){

ExSpanishSufix[] values= ExSpanishSufix.values();
Arrays.sort(values,
Comparator.comparing((ExSpanishSufix hex) -> hex.getExponent()));

return values;
}

public String isPlural(long number){

return this.name()+(number>1?plural:"");
}

public int getExponent(){

return exponent;
}

public long getValue(){

return value;
}

}

package numberTranslator;

import java.util.Arrays;
import java.util.Comparator;

public enum SpanishDigits {

cero(0), uno(1),dos(2),tres(3),cuatro(4),cinco(5),seis(6),siete(7),ocho(8),
nueve(9), dies(10), once(11), doce(12), trece(13), catorce(14), quince(15);

private int number;

private SpanishDigits(int pnumber){
number=pnumber;
}

public static SpanishDigits[] getSorted(){

SpanishDigits[] values= SpanishDigits.values();
Arrays.sort(values,
Comparator.comparing((SpanishDigits hex) -> hex.getNumber()));
return values;
}

public int getNumber(){

return number;
}

}

package numberTranslator;

import java.util.Arrays;
import java.util.Comparator;

public enum SpanishTens {

dies(10,"dieci"), veinte(20,"veinti"), treinta(30), cuarenta(40),
cincuenta(50), sesenta(60), setent

Solution

As Spanish native speaker, for thousands, millions and billions quantities there's a grammatical (Spanish translation) error related to "Uno" numbering. As Follows in the next example:

For the number: 1 481 481 461

This is the translation from the current code:

billon cuatrocientos ochenta y uno millones cuatrocientos ochenta y uno mil cuatrocientos sesenta y uno

the correct way is:
billon cuatrocientos ochenta y un millones cuatrocientos ochenta y un mil cuatrocientos sesenta y uno

The same happens with lesser quantities on thousands scale. i.e. 21 0001 "veintiuno mil uno" should be "veintiun mil uno".

The code Fix solution is below replacing the especialCases method implementation :

private String especialCases(String numero){
        if(numero.isEmpty())
            return "cero";
        
        if(numero.substring(numero.length()-3,numero.length() ).equals("uno") ) {
            numero= numero.replaceAll("uno", "un");
            numero = numero + "o";
            return numero.replaceAll("cincocientos", "quinientos")
                .replaceAll("nuevecientos", "novecientos")
                .replaceAll("sietecientos", "setecientos");
        }
        else {
                numero= numero.replaceAll("uno", "un");
            return numero.replaceAll("cincocientos", "quinientos")
            .replaceAll("nuevecientos", "novecientos")
            .replaceAll("sietecientos", "setecientos");   
        }
    }


Another bug is related with "dies" to represent "ten" based numbers. orthographic error, should be "diez". Below correction for SpanishDigits enum

enum SpanishDigits {
    cero(0), uno(1),dos(2),tres(3),cuatro(4),cinco(5),seis(6),siete(7),ocho(8),
    nueve(9), diez(10), once(11), doce(12), trece(13), catorce(14), quince(15);

    private int number;

    private SpanishDigits(int pnumber){
    number=pnumber;
    }

    public static SpanishDigits[] getSorted(){
    SpanishDigits[] values= SpanishDigits.values();
    Arrays.sort(values, 
    Comparator.comparing((SpanishDigits hex) -> hex.getNumber()));
    
    return values;
    }

    public int getNumber(){
    return number;
    }
}

Code Snippets

private String especialCases(String numero){
        if(numero.isEmpty())
            return "cero";
        
        if(numero.substring(numero.length()-3,numero.length() ).equals("uno") ) {
            numero= numero.replaceAll("uno", "un");
            numero = numero + "o";
            return numero.replaceAll("cincocientos", "quinientos")
                .replaceAll("nuevecientos", "novecientos")
                .replaceAll("sietecientos", "setecientos");
        }
        else {
                numero= numero.replaceAll("uno", "un");
            return numero.replaceAll("cincocientos", "quinientos")
            .replaceAll("nuevecientos", "novecientos")
            .replaceAll("sietecientos", "setecientos");   
        }
    }
enum SpanishDigits {
    cero(0), uno(1),dos(2),tres(3),cuatro(4),cinco(5),seis(6),siete(7),ocho(8),
    nueve(9), diez(10), once(11), doce(12), trece(13), catorce(14), quince(15);

    private int number;

    private SpanishDigits(int pnumber){
    number=pnumber;
    }

    public static SpanishDigits[] getSorted(){
    SpanishDigits[] values= SpanishDigits.values();
    Arrays.sort(values, 
    Comparator.comparing((SpanishDigits hex) -> hex.getNumber()));
    
    return values;
    }

    public int getNumber(){
    return number;
    }
}

Context

StackExchange Code Review Q#105974, answer score: 4

Revisions (0)

No revisions yet.