BigDecimal: MoneyDto
parent
2f6231da2c
commit
2dc117c978
|
@ -15,12 +15,20 @@ public class BigDecimalExample {
|
||||||
// Currency tnd = Currency.getInstance("TND");
|
// Currency tnd = Currency.getInstance("TND");
|
||||||
// System.out.println("TND:" + tnd.getDefaultFractionDigits());
|
// System.out.println("TND:" + tnd.getDefaultFractionDigits());
|
||||||
|
|
||||||
|
// BigDecimal amount = new BigDecimal("4.12");
|
||||||
|
// System.out.println(amount);
|
||||||
|
// amount = amount.movePointRight(2);
|
||||||
|
// System.out.println(amount);
|
||||||
|
// amount = amount.movePointLeft(2);
|
||||||
|
// System.out.println(amount);
|
||||||
|
|
||||||
BigDecimal amount = new BigDecimal("4.12");
|
BigDecimal amount = new BigDecimal("4.12");
|
||||||
System.out.println(amount);
|
Currency eur = Currency.getInstance("EUR");
|
||||||
amount = amount.movePointRight(2);
|
System.out.println(amount + " " + eur);
|
||||||
System.out.println(amount);
|
MoneyDto moneyDto = new MoneyDto(amount, eur);
|
||||||
amount = amount.movePointLeft(2);
|
System.out.println(moneyDto);
|
||||||
System.out.println(amount);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fromInteger() {
|
static void fromInteger() {
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package me.bvn13.test.bigdecimal;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Currency;
|
||||||
|
|
||||||
|
public class MoneyDto {
|
||||||
|
|
||||||
|
private final BigDecimal amount;
|
||||||
|
private final Currency currency;
|
||||||
|
|
||||||
|
public static MoneyDto fromMinor(BigDecimal amount, Currency currency) {
|
||||||
|
return new MoneyDto(amount.movePointLeft(currency.getDefaultFractionDigits()), currency);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MoneyDto fromJson(String amount, String cur) {
|
||||||
|
Currency currency = Currency.getInstance(cur);
|
||||||
|
return new MoneyDto(new BigDecimal(amount).movePointLeft(currency.getDefaultFractionDigits()), currency);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MoneyDto(BigDecimal amount, Currency currency) {
|
||||||
|
this.amount = amount;
|
||||||
|
this.currency = currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getAmount() {
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Currency getCurrency() {
|
||||||
|
return currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "{" +
|
||||||
|
"\"amount\":" + amount.movePointRight(currency.getDefaultFractionDigits()) +
|
||||||
|
", \"currency\":\"" + currency + "\"" +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue