public class ConvertNumber
|Modifier and Type
|Field and Description
Version number of this Package (read-only).
|Modifier and Type
|Method and Description
Converts one of the following numeric data type keywords to its expected numeric value, while keeping it in String format. This is useful for providing more flexibility to end-users in a number of places, including configuration files or user interfaces, by handling their string prior to converting it an actual primitive/native numeric data type.
public static final String VERSION
public static String typeAlias(String num)
If the keyword isn't found (and thus no conversion takes place), the original String is returned without modifications. No exception is thrown. The purpose of this method is not to eliminate all number format exceptions, but to provide a convenient way to express a few standard numbers through specialized text-based keywords. The supported keywords are:
-nybble..nybble = -8..7 -byte..byte = -128..127 -short..short = -32768..32767 -int..int = -2147483648..2147483647 -long..long = -9223372036854775808..9223372036854775807 -verylong..verylong = -170141183460469231731687303715884105728..170141183460469231731687303715884105727 -float..float = -1.40129846432481707e-45..3.40282346638528860e+38 -double..double = -4.94065645841246544e-324..1.79769313486231570e+308
You don't need to worry if a number is larger than your target data type because you still need to catch the appropriate exception(s). If you specify a range, your users who prefer to use these keywords are likely to understand the effects of using out-of-range values anyway.
Internally, an optimized order of simple "if" statements is used to compare the supplied String, which is more efficient than using a HashMap which comes with more overhead due to additional calculations that really aren't needed for simple tasks such as this.
A special note of appreciation goes to Roedy Green for his work on accurately documenting the actual minimum and maximum values of the "float" and "double" primitives in an easy-to-read chart on his web site at: http://mindprod.com/jgloss/primitive.html.
The Nybble and VeryLong types were added by Randolf Richardson.
num - The numeric string to convert