Skip to content

Commit

Permalink
#275 Using default formatting for current default locale, added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
atsticks committed Sep 12, 2019
1 parent 06b43ae commit 542b630
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 32 deletions.
11 changes: 10 additions & 1 deletion moneta-core/src/main/java/org/javamoney/moneta/FastMoney.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import org.javamoney.moneta.spi.MoneyUtils;

import javax.money.*;
import javax.money.format.AmountFormatQuery;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -557,7 +560,13 @@ public NumberValue getNumber() {

@Override
public String toString() {
return currency.toString() + ' ' + getBigDecimal();
try {
MonetaryAmount amount = Monetary.getDefaultRounding().apply(this);
MonetaryAmountFormat fmt = MonetaryFormats.getAmountFormat(Locale.getDefault());
return fmt.format(amount);
}catch(Exception e) {
return currency.toString() + ' ' + getBigDecimal();
}
}

// Internal helper methods
Expand Down
6 changes: 4 additions & 2 deletions moneta-core/src/main/java/org/javamoney/moneta/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

import javax.money.*;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Locale;
import java.util.Objects;

/**
Expand Down Expand Up @@ -669,8 +671,8 @@ public boolean equals(Object obj) {
public String toString() {
try {
MonetaryAmount amount = Monetary.getDefaultRounding().apply(this);
return getCurrency().getCurrencyCode() + ' ' + amount.getNumber().numberValue(BigDecimal.class)
.toPlainString();
MonetaryAmountFormat fmt = MonetaryFormats.getAmountFormat(Locale.getDefault());
return fmt.format(amount);
}catch(Exception e) {
return getCurrency().getCurrencyCode() + ' ' + number.toPlainString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

import javax.money.*;
import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

Expand Down Expand Up @@ -756,7 +758,13 @@ public <T> T asType(Class<T> type, MonetaryOperator adjuster) {
*/
@Override
public String toString() {
return currency.getCurrencyCode() + ' ' + number;
try {
MonetaryAmount amount = Monetary.getDefaultRounding().apply(this);
MonetaryAmountFormat fmt = MonetaryFormats.getAmountFormat(Locale.getDefault());
return fmt.format(amount);
}catch(Exception e) {
return currency.getCurrencyCode() + ' ' + number;
}
}

/*
Expand Down
17 changes: 12 additions & 5 deletions moneta-core/src/test/java/org/javamoney/moneta/FastMoneyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.invoke.MethodHandles;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -1166,11 +1167,17 @@ public void testGetNumber() {
*/
@Test
public void testToString() {
assertEquals("XXX 1.23455", FastMoney.of(new BigDecimal("1.23455"), "XXX").toString());
assertEquals("CHF 1234.00000", FastMoney.of(1234, "CHF").toString());
assertEquals("CHF 1234.00000", FastMoney.of(new BigDecimal("1234.0"), "CHF").toString());
assertEquals("CHF 1234.10000", FastMoney.of(new BigDecimal("1234.1"), "CHF").toString());
assertEquals("CHF 0.01000", FastMoney.of(new BigDecimal("0.0100"), "CHF").toString());
Locale defaultLocale = Locale.getDefault();
try{
Locale.setDefault(Locale.GERMANY);
assertEquals("0,00 XXX", FastMoney.of(new BigDecimal("1.23455"), "XXX").toString());
assertEquals("1.234,00 CHF", FastMoney.of(1234, "CHF").toString());
assertEquals("1.234,00 CHF", FastMoney.of(new BigDecimal("1234.0"), "CHF").toString());
assertEquals("1.234,10 CHF", FastMoney.of(new BigDecimal("1234.1"), "CHF").toString());
assertEquals("0,01 CHF", FastMoney.of(new BigDecimal("0.0100"), "CHF").toString());
}finally{
Locale.setDefault(defaultLocale);
}
}

/**
Expand Down
21 changes: 14 additions & 7 deletions moneta-core/src/test/java/org/javamoney/moneta/MoneyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Locale;

import javax.money.*;

Expand Down Expand Up @@ -1073,13 +1074,19 @@ public void testStripTrailingZeroes() {
*/
@Test
public void testToString() {
assertEquals("XXX 1.23455645", Money.of(new BigDecimal("1.23455645"), "XXX").toString());
assertEquals("CHF 1234", Money.of(1234, "CHF").toString());
assertEquals("CHF 1234", Money.of(new BigDecimal("1234.0"), "CHF").toString());
assertEquals("CHF 1234.1", Money.of(new BigDecimal("1234.1"), "CHF").toString());
assertEquals("CHF 0.01", Money.of(new BigDecimal("0.0100"), "CHF").toString());
assertEquals("CHF 50",
Money.of(new BigDecimal("500").multiply(new BigDecimal(".1")), "CHF").toString());
Locale defaultLocale = Locale.getDefault();
try{
Locale.setDefault(Locale.GERMANY);
assertEquals("0,00 XXX", Money.of(new BigDecimal("1.23455645"), "XXX").toString());
assertEquals("1.234,00 CHF", Money.of(1234, "CHF").toString());
assertEquals("1.234,00 CHF", Money.of(new BigDecimal("1234.0"), "CHF").toString());
assertEquals("1.234,10 CHF", Money.of(new BigDecimal("1234.1"), "CHF").toString());
assertEquals("0,01 CHF", Money.of(new BigDecimal("0.0100"), "CHF").toString());
assertEquals("50,00 CHF",
Money.of(new BigDecimal("500").multiply(new BigDecimal(".1")), "CHF").toString());
}finally{
Locale.setDefault(defaultLocale);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Locale;

import javax.money.*;

Expand Down Expand Up @@ -960,11 +961,17 @@ public void testGetNumber() {
*/
@Test
public void testToString() {
assertEquals("XXX 1.23455645", RoundedMoney.of(new BigDecimal("1.23455645"), "XXX").toString());
assertEquals("CHF 1234", RoundedMoney.of(1234, "CHF").toString());
assertEquals("CHF 1234", RoundedMoney.of(new BigDecimal("1234.0"), "CHF").toString());
assertEquals("CHF 1234.1", RoundedMoney.of(new BigDecimal("1234.1"), "CHF").toString());
assertEquals("CHF 0.01", RoundedMoney.of(new BigDecimal("0.0100"), "CHF").toString());
Locale defaultLocale = Locale.getDefault();
try{
Locale.setDefault(Locale.ENGLISH);
assertEquals("XXX0.00", RoundedMoney.of(new BigDecimal("1.23455645"), "XXX").toString());
assertEquals("CHF1,234.00", RoundedMoney.of(1234, "CHF").toString());
assertEquals("CHF1,234.00", RoundedMoney.of(new BigDecimal("1234.0"), "CHF").toString());
assertEquals("CHF1,234.10", RoundedMoney.of(new BigDecimal("1234.1"), "CHF").toString());
assertEquals("CHF0.01", RoundedMoney.of(new BigDecimal("0.0100"), "CHF").toString());
}finally{
Locale.setDefault(defaultLocale);
}
}

// /**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void shoudReturNullStringOnQueryFromWhenMonetaryIsNullWithFastMoney() {
public void shoudReturnToStringOnQueryFromWhenMonetaryWithFastMoney() {
MonetaryAmount money = Money.of(10, BRAZILIAN_REAL);
String result = ToStringMonetaryAmountFormat.of(ToStringMonetaryAmountFormatStyle.FAST_MONEY).queryFrom(money);
assertEquals(result, "BRL 10");
assertEquals(result, "BRL 10.00");
}

@Test
Expand All @@ -123,7 +123,7 @@ public void shoudReturNullStringOnQueryFromWhenMonetaryIsNullWithMoney() {
public void shoudReturnToStringOnQueryFromWhenMonetaryWithMoney() {
MonetaryAmount money = Money.of(10, BRAZILIAN_REAL);
String result = ToStringMonetaryAmountFormat.of(ToStringMonetaryAmountFormatStyle.MONEY).queryFrom(money);
assertEquals(result, "BRL 10");
assertEquals(result, "BRL 10.00");
}

@Test
Expand All @@ -136,7 +136,7 @@ public void shoudReturNullStringOnQueryFromWhenMonetaryIsNullWithRoundedMoney()
public void shoudReturnToStringOnQueryFromWhenMonetaryWithRoundedMoney() {
MonetaryAmount money = Money.of(10, BRAZILIAN_REAL);
String result = ToStringMonetaryAmountFormat.of(ToStringMonetaryAmountFormatStyle.ROUNDED_MONEY).queryFrom(money);
assertEquals(result, "BRL 10");
assertEquals(result, "BRL 10.00");
}
@Test
public void shoudReturNullStringOnPrintWhenMonetaryIsNullWithFastMoney() throws IOException {
Expand All @@ -150,7 +150,7 @@ public void shoudReturnToStringOnPrintWhenHasMonetaryWithFastMoney() throws IOEx
StringBuilder sb = new StringBuilder();
MonetaryAmount money = Money.of(10, BRAZILIAN_REAL);
ToStringMonetaryAmountFormat.of(ToStringMonetaryAmountFormatStyle.FAST_MONEY).print(sb, money);
assertEquals(sb.toString(), "BRL 10");
assertEquals(sb.toString(), "BRL 10.00");
}
//
@Test
Expand All @@ -165,7 +165,7 @@ public void shoudReturnToStringOnPrintWhenHasMonetaryWithMoney() throws IOExcept
StringBuilder sb = new StringBuilder();
MonetaryAmount money = Money.of(10, BRAZILIAN_REAL);
ToStringMonetaryAmountFormat.of(ToStringMonetaryAmountFormatStyle.MONEY).print(sb, money);
assertEquals(sb.toString(), "BRL 10");
assertEquals(sb.toString(), "BRL 10.00");
}
@Test
public void shoudReturNullStringOnPrintWhenMonetaryIsNullWithRoundedMoney() throws IOException {
Expand All @@ -179,7 +179,7 @@ public void shoudReturnToStringOnPrintWhenHasMonetaryWithRoundedMoney() throws I
StringBuilder sb = new StringBuilder();
MonetaryAmount money = Money.of(10, BRAZILIAN_REAL);
ToStringMonetaryAmountFormat.of(ToStringMonetaryAmountFormatStyle.ROUNDED_MONEY).print(sb, money);
assertEquals(sb.toString(), "BRL 10");
assertEquals(sb.toString(), "BRL 10.00");
}

private void executeTest(MonetaryAmount expectedMoney, MonetaryAmount a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testParse() {
MonetaryAmount parsedAmount = format.parse("USD1,000.42");
assertEquals(parsedAmount.getCurrency().getCurrencyCode(), "USD");
assertEquals(parsedAmount.getNumber().doubleValueExact(), 1000.42D);
assertEquals(parsedAmount.toString(), "USD 1000.42");
assertEquals(parsedAmount.toString(), "USD 1'000.42");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void testFormat() {
assertEquals("123.456.789.101.112,12 INR", defaultFormat.format(amountInr));
defaultFormat = MonetaryFormats.getAmountFormat(new Locale("", "IN"));
MonetaryAmount amountChf = Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(1211112.50).create();
assertEquals("CHF 1,211,112.50", defaultFormat.format(amountChf));
assertEquals("INR 123,456,789,101,112.12", defaultFormat.format(amountInr));
assertEquals("CHF 12,11,112.50", defaultFormat.format(amountChf));
assertEquals("INR 12,34,56,78,91,01,112.12", defaultFormat.format(amountInr));
// Locale india = new Locale("", "IN");
// defaultFormat = MonetaryFormats.getAmountFormatBuilder(india)
// .setNumberGroupSizes(3, 2).of();
Expand Down Expand Up @@ -108,11 +108,11 @@ public void testPrint() throws IOException {
defaultFormat = MonetaryFormats.getAmountFormat(new Locale("", "IN"));
defaultFormat
.print(b, Monetary.getDefaultAmountFactory().setCurrency("CHF").setNumber(1211112.50).create());
assertEquals("CHF 1,211,112.50", b.toString());
assertEquals("CHF 12,11,112.50", b.toString());
b.setLength(0);
defaultFormat.print(b, Monetary.getDefaultAmountFactory().setCurrency("INR")
.setNumber(123456789101112.123456).create());
assertEquals("INR 123,456,789,101,112.12", b.toString());
assertEquals("INR 12,34,56,78,91,01,112.12", b.toString());
b.setLength(0);
// Locale india = new Locale("", "IN");
// defaultFormat = MonetaryFormats.getAmountFormat(india)
Expand Down

0 comments on commit 542b630

Please # to comment.