Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix - spanish money converters #152

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
jglaszka marked this conversation as resolved.
Show resolved Hide resolved
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");
}
Comment on lines +27 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another option is to create a separate IntegerToStringConverter just for spanish bankingMoney conversion

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on the other hand, there's plenty of replaceAll usages in this project so that wouldn't be an excpetion

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with first proposition but it's up to you - second one is not that ugly in the end.

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"() {
jglaszka marked this conversation as resolved.
Show resolved Hide resolved
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"
}
}