BitcoinValue { public static long userInputToSatoshis(String userInput) throws NumberFormatException { BigDecimal bd = new BigDecimal(userInput.trim()); if (bd.signum() < 0) throw new NumberFormatException("Negative numbers not allowed"); final BigDecimal satoshis = bd.movePointRight(8); if (satoshis.scale() > 0) throw new NumberFormatException("Fractional value units not allowed"); final long l = satoshis.longValue(); if (l > NetworkParameters.MAX_MONEY.longValue()) throw new NumberFormatException("Larger than MAX_MONEY"); return l; }  static long userInputToSatoshis(String userInput);  }
@Test public void userInputOK() throws Exception { assertEquals(100_000_000, userInputToSatoshis("1.0")); assertEquals(0, userInputToSatoshis("0.0")); assertEquals(0, userInputToSatoshis("0")); assertEquals(1, userInputToSatoshis("0.00000001")); assertEquals(21_000_000L * 100_000_000L, userInputToSatoshis("21000000")); }
@Test(expected = NumberFormatException.class) public void tooSmall() throws Exception { userInputToSatoshis("0.000000001"); }
@Test(expected = NumberFormatException.class) public void tooLarge() throws Exception { userInputToSatoshis("21000000.1"); }
@Test(expected = NumberFormatException.class) public void tooNotNumeric() throws Exception { userInputToSatoshis("1.0 mBTC"); }