Skip to content

Commit 2dc117c

Browse files
committed
BigDecimal: MoneyDto
1 parent 2f6231d commit 2dc117c

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

big-decimal/src/main/java/me/bvn13/test/bigdecimal/BigDecimalExample.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ public static void main(String[] args) {
1515
// Currency tnd = Currency.getInstance("TND");
1616
// System.out.println("TND:" + tnd.getDefaultFractionDigits());
1717

18+
// BigDecimal amount = new BigDecimal("4.12");
19+
// System.out.println(amount);
20+
// amount = amount.movePointRight(2);
21+
// System.out.println(amount);
22+
// amount = amount.movePointLeft(2);
23+
// System.out.println(amount);
24+
1825
BigDecimal amount = new BigDecimal("4.12");
19-
System.out.println(amount);
20-
amount = amount.movePointRight(2);
21-
System.out.println(amount);
22-
amount = amount.movePointLeft(2);
23-
System.out.println(amount);
26+
Currency eur = Currency.getInstance("EUR");
27+
System.out.println(amount + " " + eur);
28+
MoneyDto moneyDto = new MoneyDto(amount, eur);
29+
System.out.println(moneyDto);
30+
31+
2432
}
2533

2634
static void fromInteger() {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package me.bvn13.test.bigdecimal;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Currency;
5+
6+
public class MoneyDto {
7+
8+
private final BigDecimal amount;
9+
private final Currency currency;
10+
11+
public static MoneyDto fromMinor(BigDecimal amount, Currency currency) {
12+
return new MoneyDto(amount.movePointLeft(currency.getDefaultFractionDigits()), currency);
13+
}
14+
15+
public static MoneyDto fromJson(String amount, String cur) {
16+
Currency currency = Currency.getInstance(cur);
17+
return new MoneyDto(new BigDecimal(amount).movePointLeft(currency.getDefaultFractionDigits()), currency);
18+
}
19+
20+
public MoneyDto(BigDecimal amount, Currency currency) {
21+
this.amount = amount;
22+
this.currency = currency;
23+
}
24+
25+
public BigDecimal getAmount() {
26+
return amount;
27+
}
28+
29+
public Currency getCurrency() {
30+
return currency;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "{" +
36+
"\"amount\":" + amount.movePointRight(currency.getDefaultFractionDigits()) +
37+
", \"currency\":\"" + currency + "\"" +
38+
'}';
39+
}
40+
}

0 commit comments

Comments
 (0)