NumberUtils { @SuppressWarnings("unchecked") public static <T> T parse(final String text, final Class<?> clazz) { assertNotNull(text, "Text must not be null"); assertNotNull(clazz, "Target class must not be null"); final String trimmed = StringUtils.trim(text); if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { return (T) Byte.decode(trimmed); } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { return (T) Short.decode(trimmed); } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { return (T) Integer.decode(trimmed); } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { return (T) Long.decode(trimmed); } else if (clazz.equals(BigInteger.class)) { return (T) decodeBigInteger(trimmed); } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { return (T) Float.valueOf(trimmed); } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { return (T) Double.valueOf(trimmed); } else if (clazz.equals(BigDecimal.class) || clazz.equals(Number.class)) { return (T) new BigDecimal(trimmed); } else { throw new IllegalArgumentException( "Cannot convert String [" + text + "] to target class [" + clazz.getName() + "]"); } }  @SuppressWarnings("unchecked") static T convert(final Number number, final Class<T> targetClass); @SuppressWarnings("unchecked") static T parse(final String text, final Class<?> clazz); static T parse(final String text, final Class<T> targetClass,
      final NumberFormat numberFormat); static byte[] positiveToByteArray(final BigInteger positiveNumber); static BigInteger byteArrayToPositive(final byte[] rawBytes);  }
@Test public void testParse() throws ClassNotFoundException { final Object[][] testParameters = new Object[][] {{"127", Byte.class, (byte) 127}, {"128", Short.class, (short) 128}, {"255", Integer.class, 255}, {"256", Long.class, 256L}, {"511", BigInteger.class, BigInteger.valueOf(511)}, {"512", BigInteger.class, new BigDecimal("512").toBigInteger()}, {"1023", Float.class, 1023f}, {"1024", Double.class, 1024d}, {"2047", BigDecimal.class, new BigDecimal("2047")}}; for (final Object[] testParameter : testParameters) { Object expected = Class.forName(testParameter[2].getClass().getName()).cast(testParameter[2]); String text = (String) testParameter[0]; Class<?> clazz = (Class<?>) testParameter[1]; assertEquals(expected, NumberUtils.parse(text, clazz)); } }
@Test public void testParseOnNumberFormat() throws ClassNotFoundException { final Object[][] testParameters = new Object[][] { {"127", Byte.class, NumberFormat.getInstance(), (byte) 127}, {"1,280", Short.class, NumberFormat.getInstance(), (short) 1280}, {"25,500", Integer.class, NumberFormat.getInstance(), 25500}, {"256,000", Long.class, NumberFormat.getInstance(), 256000L}, {"5,110,000", BigInteger.class, new DecimalFormat(), BigInteger.valueOf(5110000)}, {"51,200,000", BigInteger.class, new DecimalFormat(), new BigDecimal("51200000").toBigInteger()}, {"10,230,000.1023", Float.class, new DecimalFormat(), 10230000.1023f}, {"10,240,000.1024", Double.class, new DecimalFormat(), 10240000.1024d}, {"204,700,000.2047", BigDecimal.class, new DecimalFormat(), new BigDecimal("204700000.2047")}}; assertNotNull(NumberUtils.parse("127", Byte.class, null)); for (final Object[] testParameter : testParameters) { Object expected = Class.forName(testParameter[3].getClass().getName()).cast(testParameter[3]); String text = (String) testParameter[0]; Class<?> targetClass = (Class<?>) testParameter[1]; NumberFormat numberFormat = (NumberFormat) testParameter[2]; assertEquals(expected, NumberUtils.parse(text, targetClass, numberFormat)); } }