mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-16 04:05:50 +00:00
Monetary: Add parseCoinInexact() and parseFiatInexact() variants that will cut the amount to the smallest unit.
This commit is contained in:
parent
9c72c86ed1
commit
34e33a194b
@ -116,12 +116,16 @@ public final class Coin implements Monetary, Comparable<Coin>, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an amount expressed in the way humans are used to.<p>
|
* <p>
|
||||||
|
* Parses an amount expressed in the way humans are used to.
|
||||||
* <p/>
|
* <p/>
|
||||||
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)},
|
* <p>
|
||||||
* for example "0", "1", "0.10", "1.23E3", "1234.5E-5".
|
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)}, for example "0", "1", "0.10",
|
||||||
|
* "1.23E3", "1234.5E-5".
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException if you try to specify fractional satoshis, or a value out of range.
|
* @throws IllegalArgumentException
|
||||||
|
* if you try to specify fractional satoshis, or a value out of range.
|
||||||
*/
|
*/
|
||||||
public static Coin parseCoin(final String str) {
|
public static Coin parseCoin(final String str) {
|
||||||
try {
|
try {
|
||||||
@ -132,6 +136,27 @@ public final class Coin implements Monetary, Comparable<Coin>, Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Parses an amount expressed in the way humans are used to. The amount is cut to satoshi precision.
|
||||||
|
* <p/>
|
||||||
|
* <p>
|
||||||
|
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)}, for example "0", "1", "0.10",
|
||||||
|
* "1.23E3", "1234.5E-5".
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
* if you try to specify a value out of range.
|
||||||
|
*/
|
||||||
|
public static Coin parseCoinInexact(final String str) {
|
||||||
|
try {
|
||||||
|
long satoshis = new BigDecimal(str).movePointRight(SMALLEST_UNIT_EXPONENT).longValue();
|
||||||
|
return Coin.valueOf(satoshis);
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
throw new IllegalArgumentException(e); // Repackage exception to honor method contract
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Coin add(final Coin value) {
|
public Coin add(final Coin value) {
|
||||||
return new Coin(LongMath.checkedAdd(this.value, value.value));
|
return new Coin(LongMath.checkedAdd(this.value, value.value));
|
||||||
}
|
}
|
||||||
|
@ -73,11 +73,13 @@ public final class Fiat implements Monetary, Comparable<Fiat>, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an amount expressed in the way humans are used to.
|
|
||||||
* <p>
|
* <p>
|
||||||
|
* Parses an amount expressed in the way humans are used to.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
* <p>
|
||||||
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)}, for example "0", "1", "0.10",
|
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)}, for example "0", "1", "0.10",
|
||||||
* "1.23E3", "1234.5E-5".
|
* "1.23E3", "1234.5E-5".
|
||||||
|
* <p/>
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException
|
||||||
* if you try to specify more than 4 digits after the comma, or a value out of range.
|
* if you try to specify more than 4 digits after the comma, or a value out of range.
|
||||||
@ -91,6 +93,27 @@ public final class Fiat implements Monetary, Comparable<Fiat>, Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Parses an amount expressed in the way humans are used to. The amount is cut to 4 digits after the comma.
|
||||||
|
* <p/>
|
||||||
|
* <p>
|
||||||
|
* This takes string in a format understood by {@link BigDecimal#BigDecimal(String)}, for example "0", "1", "0.10",
|
||||||
|
* "1.23E3", "1234.5E-5".
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
* if you try to specify a value out of range.
|
||||||
|
*/
|
||||||
|
public static Fiat parseFiatInexact(final String currencyCode, final String str) {
|
||||||
|
try {
|
||||||
|
long val = new BigDecimal(str).movePointRight(SMALLEST_UNIT_EXPONENT).longValue();
|
||||||
|
return Fiat.valueOf(currencyCode, val);
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Fiat add(final Fiat value) {
|
public Fiat add(final Fiat value) {
|
||||||
checkArgument(value.currencyCode.equals(currencyCode));
|
checkArgument(value.currencyCode.equals(currencyCode));
|
||||||
return new Fiat(currencyCode, LongMath.checkedAdd(this.value, value.value));
|
return new Fiat(currencyCode, LongMath.checkedAdd(this.value, value.value));
|
||||||
|
@ -42,6 +42,19 @@ public class CoinTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
org.junit.Assert.fail("should throw IllegalArgumentException");
|
org.junit.Assert.fail("should throw IllegalArgumentException");
|
||||||
}
|
}
|
||||||
|
assertEquals(1, parseCoin("0.00000001").value);
|
||||||
|
assertEquals(1, parseCoin("0.000000010").value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testParseCoinOverprecise() {
|
||||||
|
parseCoin("0.000000011");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseCoinInexact() {
|
||||||
|
assertEquals(1, parseCoinInexact("0.00000001").value);
|
||||||
|
assertEquals(1, parseCoinInexact("0.000000011").value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -31,6 +31,23 @@ public class FiatTest {
|
|||||||
assertEquals(Fiat.valueOf("EUR", -10000), parseFiat("EUR", "-1"));
|
assertEquals(Fiat.valueOf("EUR", -10000), parseFiat("EUR", "-1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseFiat() {
|
||||||
|
assertEquals(1, Fiat.parseFiat("EUR", "0.0001").value);
|
||||||
|
assertEquals(1, Fiat.parseFiat("EUR", "0.00010").value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testParseFiatOverprecise() {
|
||||||
|
Fiat.parseFiat("EUR", "0.00011");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseFiatInexact() {
|
||||||
|
assertEquals(1, Fiat.parseFiatInexact("EUR", "0.0001").value);
|
||||||
|
assertEquals(1, Fiat.parseFiatInexact("EUR", "0.00011").value);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToFriendlyString() {
|
public void testToFriendlyString() {
|
||||||
assertEquals("1.00 EUR", parseFiat("EUR", "1").toFriendlyString());
|
assertEquals("1.00 EUR", parseFiat("EUR", "1").toFriendlyString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user