Skip to content

Commit

Permalink
Bugfix - spanish money converters (#152)
Browse files Browse the repository at this point in the history
* bugfix for spanish numerals + nouns

* clean not needed leftovers

* clean not needed leftovers

* clean unused imports
  • Loading branch information
jglaszka authored Apr 9, 2024
1 parent 3f6438b commit 32c13f7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValuesForSmallNumbers;
import pl.allegro.finance.tradukisto.internal.languages.slovene.SloveneThousandToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.slovene.SloveneValues;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishBigDecimalToBankingMoneyConverter;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishIntegerToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishIntegerToWordsConverterAdapter;
import pl.allegro.finance.tradukisto.internal.languages.spanish.SpanishThousandToWordsConverter;
Expand Down Expand Up @@ -297,7 +298,7 @@ public static Container spanishContainer() {
spanishThousandToWordsConverter
);

BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter = new BigDecimalToBankingMoneyConverter(
BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter = new SpanishBigDecimalToBankingMoneyConverter(
converter,
values.currency()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String asWords(BigDecimal value, String currencySymbol) {
return format(FORMAT, converter.asWords(units), currencySymbol, subunits);
}

private void validate(BigDecimal value) {
protected void validate(BigDecimal value) {
Assert.isTrue(value.scale() <= MAXIMAL_DECIMAL_PLACES_COUNT,
() -> String.format("can't transform more than %s decimal places for value %s", MAXIMAL_DECIMAL_PLACES_COUNT, value));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.allegro.finance.tradukisto.internal.languages.spanish;

import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.converters.BigDecimalToBankingMoneyConverter;

import java.math.BigDecimal;

import static java.lang.String.format;

public class SpanishBigDecimalToBankingMoneyConverter extends BigDecimalToBankingMoneyConverter {
private static final String FORMAT = "%s %s %02d/100";
private final IntegerToStringConverter converter;

public SpanishBigDecimalToBankingMoneyConverter(IntegerToStringConverter converter, String currencySymbol) {
super(converter, currencySymbol);
this.converter = converter;
}

@Override
public String asWords(BigDecimal value, String currencySymbol) {
validate(value);
Integer units = value.intValue();
Integer subunits = value.remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).intValue();

String words = converter.asWords(units);

// bugfix - ugly hack for ending with "1" :( numerals + nouns needs to have specific gender form
// for example "51 euros" - it is needed to use "cincuenta y un" instead of "cincuenta y uno"
// converter itself does not have context if it is formatting for "standalone" form or with noun
if (words.endsWith("uno")) {
words = words.replaceAll("uno$","un");
}
return format(FORMAT, words, currencySymbol, subunits);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SpanishValuesTest extends Specification {

static intConverter = spanishContainer().getIntegerConverter()
static longConverter = spanishContainer().getLongConverter()
static moneyConverter = spanishContainer().getBankingMoneyConverter()

@Unroll
def "should convert #value to '#words' in Spanish"() {
Expand Down Expand Up @@ -145,4 +146,24 @@ class SpanishValuesTest extends Specification {
2_000_000_000_000_000_000 | ""
Long.MAX_VALUE | ""
}
}

@Unroll
def "should convert money #value to '#words' in Spanish"() {
expect:
moneyConverter.asWords(value, "") == words

where:
value | words
51 | "cincuenta y un € 00/100"
111.12 | "ciento once € 12/100"
112.12 | "ciento doce € 12/100"
123.29 | "ciento veintitrés € 29/100"
154.63 | "ciento cincuenta y cuatro € 63/100"
415.51 | "cuatrocientos quince € 51/100"
426.83 | "cuatrocientos veintiséis € 83/100"
447.60 | "cuatrocientos cuarenta y siete € 60/100"
1298 | "mil doscientos noventa y ocho € 00/100"
1299 | "mil doscientos noventa y nueve € 00/100"
1111111.11 | "un millón ciento once mil ciento once € 11/100"
}
}

0 comments on commit 32c13f7

Please sign in to comment.