public static Number toNumber ( String str , Number defaultValue ) { try { // float if ( str . indexOf ( ' ' ) != - 1 ) { return new BigDecimal ( str ) ; } // integer BigInteger bi = new BigInteger ( str ) ; int l = bi . bitLength ( ) ; if ( l < 32 ) return new Integer ( bi . intValue ( ) ) ; if ( l < 64 ) return new Long ( bi . longValue ( ) ) ; return bi ; } catch ( Throwable t ) { ExceptionUtil . rethrowIfNecessary ( t ) ; return defaultValue ; } }
returns a number Object this can be a BigDecimal BigInteger Long Double depending on the input .