/** * Convert an amount expressed in the way humans are used to into units. */ public static Value valueOf(final ValueType type, final int coins, final int cents) { checkArgument(cents < 100); checkArgument(cents >= 0); checkArgument(coins >= 0); return type.oneCoin().multiply(coins).add(type.oneCoin().divide(100).multiply(cents)); }
public ExchangeRateBase(ValueType type1, ValueType type2, String rateString) { // Make the rate having maximum of RATE_SCALE decimal places BigDecimal rate = new BigDecimal(rateString) .setScale(RATE_SCALE, RoundingMode.HALF_UP).stripTrailingZeros(); checkState(rate.signum() >= 0); // rate cannot be negative // Check if the rate has too many decimal places or scale() for the type2 to handle. if (rate.scale() > type2.getUnitExponent()) { // If we have too many decimal places, multiply everything by a factor so that the rate // can fit in a type2 value. For example if the rate is 0.123456789 but a type2 can only // handle 4 places, then final result will be value1 = 100000 and value2 = 12345.6789 BigDecimal rateFactor = BigDecimal.TEN.pow(rate.scale() - type2.getUnitExponent()); value1 = type1.oneCoin().multiply(rateFactor.longValue()); value2 = Value.parse(type2, rate.multiply(rateFactor)); } else { value1 = type1.oneCoin(); value2 = Value.parse(type2, rate); } checkNonZero(value1); checkNonZero(value2); }
public void runTestParseValue2(ValueType type) { // BigDecimal version Value cent = type.oneCoin().divide(100); BigDecimal bigDecimalCent = BigDecimal.ONE.divide(new BigDecimal(100)); assertEquals(cent, parse(type, bigDecimalCent)); assertEquals(type.oneCoin().add(cent), parse(type, BigDecimal.ONE.add(bigDecimalCent))); assertEquals(type.oneCoin().negate(), parse(type, BigDecimal.ONE.negate())); try { parse(type, new BigDecimal("2E-20")); org.junit.Assert.fail("should not have accepted fractional satoshis"); } catch (ArithmeticException e) {} }
public void runTestParseValue(ValueType type) { // String version Value cent = type.oneCoin().divide(100); assertEquals(cent, parse(type, "0.01")); assertEquals(cent, parse(type, "1E-2")); assertEquals(type.oneCoin().add(cent), parse(type, "1.01")); assertEquals(type.oneCoin().negate(), parse(type, "-1")); try { parse(type, "2E-20"); org.junit.Assert.fail("should not have accepted fractional satoshis"); } catch (ArithmeticException e) {} }
@Test public void parse() throws Exception { ValueType type = BitcoinMain.get(); Value coin = type.oneCoin(); Value cent = coin.divide(100); Value millicoin = coin.divide(1000); Value microcoin = millicoin.divide(1000); assertEquals(coin, NO_CODE.parse(type, "1")); assertEquals(coin, NO_CODE.parse(type, "1.")); assertEquals(coin, NO_CODE.parse(type, "1.0")); assertEquals(coin, NO_CODE.decimalMark(',').parse(type, "1,0")); assertEquals(coin, NO_CODE.parse(type, "01.0000000000")); assertEquals(coin, NO_CODE.positiveSign('+').parse(type, "+1.0")); assertEquals(coin.negate(), NO_CODE.parse(type, "-1")); assertEquals(coin.negate(), NO_CODE.parse(type, "-1.0")); assertEquals(cent, NO_CODE.parse(type, ".01")); assertEquals(millicoin, MonetaryFormat.MBTC.parse(type, "1")); assertEquals(millicoin, MonetaryFormat.MBTC.parse(type, "1.0")); assertEquals(millicoin, MonetaryFormat.MBTC.parse(type, "01.0000000000")); assertEquals(millicoin, MonetaryFormat.MBTC.positiveSign('+').parse(type, "+1.0")); assertEquals(millicoin.negate(), MonetaryFormat.MBTC.parse(type, "-1")); assertEquals(millicoin.negate(), MonetaryFormat.MBTC.parse(type, "-1.0")); assertEquals(microcoin, MonetaryFormat.UBTC.parse(type, "1")); assertEquals(microcoin, MonetaryFormat.UBTC.parse(type, "1.0")); assertEquals(microcoin, MonetaryFormat.UBTC.parse(type, "01.0000000000")); assertEquals(microcoin, MonetaryFormat.UBTC.positiveSign('+').parse(type, "+1.0")); assertEquals(microcoin.negate(), MonetaryFormat.UBTC.parse(type, "-1")); assertEquals(microcoin.negate(), MonetaryFormat.UBTC.parse(type, "-1.0")); assertEquals(cent, NO_CODE.withLocale(new Locale("hi", "IN")).parse(type, ".०१")); // Devanagari }