org.apache.commons.lang.math.NumberUtils 메소드
apache common에 NumberUtils 메소드 중에, to타입이라는 타입 변환 메소드가 있다.
또한 숫자인지 구분하는 것과 간단히 max, min을 구하는 메소드가 있어서 소개한다.
org.junit.Assert.assertEquals(1111L, org.apache.commons.lang.math.NumberUtils.toLong("1111"));
org.junit.Assert.assertEquals(0, org.apache.commons.lang.math.NumberUtils.toLong("111a"));
org.junit.Assert.assertEquals(1.1, org.apache.commons.lang.math.NumberUtils.toDouble("1.1"), 0);
org.junit.Assert.assertEquals(3000, org.apache.commons.lang.math.NumberUtils.toInt("3000"));
org.junit.Assert.assertEquals(0, org.apache.commons.lang.math.NumberUtils.toInt("abc"));
org.junit.Assert.assertEquals(false, org.apache.commons.lang.math.NumberUtils.isDigits("a1"));
org.junit.Assert.assertEquals(true, org.apache.commons.lang.math.NumberUtils.isNumber("111"));
org.junit.Assert.assertEquals(3, org.apache.commons.lang.math.NumberUtils.max(1, 2, 3));
org.junit.Assert.assertEquals(1, org.apache.commons.lang.math.NumberUtils.min(new int[]{1,2,3}));
//모두 통과
그리고, NumberUtils에는 큰 숫자를 만들 수 있는 'create타입'이 있는데, 그 중 createBigDecimal()과 createBigInteger() 메소드가 있어서 종종 유용하다.
System.out.println(org.apache.commons.lang.math.NumberUtils.createBigDecimal("11111111111111111111111111111111111111"));
System.out.println(org.apache.commons.lang.math.NumberUtils.createBigInteger("11111111111111111111111111111111111111"));
<결과>
11111111111111111111111111111111111111
11111111111111111111111111111111111111