public final class NumberInput extends Object
NOTE! Does NOT validate against maximum length limits: caller must do that if and as necessary.
Modifier and Type | Field and Description |
---|---|
static String |
NASTY_SMALL_DOUBLE
Deprecated.
Since 2.14 -- do not use
|
Constructor and Description |
---|
NumberInput() |
Modifier and Type | Method and Description |
---|---|
static boolean |
inLongRange(char[] ch,
int off,
int len,
boolean negative)
Helper method for determining if given String representation of
an integral number would fit in 64-bit Java long or not.
|
static boolean |
inLongRange(String s,
boolean negative)
Similar to
inLongRange(char[],int,int,boolean) , but
with String argument |
static boolean |
looksLikeValidNumber(String s)
Method called to check whether given pattern looks like a valid Java
Number (which is bit looser definition than valid JSON Number).
|
static double |
parseAsDouble(String s,
double def) |
static double |
parseAsDouble(String s,
double def,
boolean useFastParser) |
static int |
parseAsInt(String s,
int def) |
static long |
parseAsLong(String s,
long def) |
static BigDecimal |
parseBigDecimal(char[] ch)
Deprecated.
Since 2.17 use
parseBigDecimal(char[], boolean) instead |
static BigDecimal |
parseBigDecimal(char[] ch,
boolean useFastParser) |
static BigDecimal |
parseBigDecimal(char[] ch,
int off,
int len)
Deprecated.
Since 2.17 use
parseBigDecimal(char[], int, int, boolean) instead |
static BigDecimal |
parseBigDecimal(char[] ch,
int off,
int len,
boolean useFastParser) |
static BigDecimal |
parseBigDecimal(String s)
Deprecated.
Since 2.17 use
parseBigDecimal(String, boolean) instead |
static BigDecimal |
parseBigDecimal(String s,
boolean useFastParser) |
static BigInteger |
parseBigInteger(String s)
Deprecated.
Since 2.17 use
parseBigInteger(String, boolean) instead |
static BigInteger |
parseBigInteger(String s,
boolean useFastParser) |
static BigInteger |
parseBigIntegerWithRadix(String s,
int radix,
boolean useFastParser) |
static double |
parseDouble(String s)
Deprecated.
Since 2.17 use
parseDouble(String, boolean) instead |
static double |
parseDouble(String s,
boolean useFastParser) |
static float |
parseFloat(String s)
Deprecated.
Since 2.17 use
parseFloat(String, boolean) instead |
static float |
parseFloat(String s,
boolean useFastParser) |
static int |
parseInt(char[] ch,
int off,
int len)
Fast method for parsing unsigned integers that are known to fit into
regular 32-bit signed int type.
|
static int |
parseInt(String s)
Helper method to (more) efficiently parse integer numbers from
String values.
|
static long |
parseLong(char[] ch,
int off,
int len) |
static long |
parseLong(String s)
Similar to
parseInt(String) but for long values. |
static long |
parseLong19(char[] ch,
int off,
boolean negative)
Parses an unsigned long made up of exactly 19 digits.
|
@Deprecated public static final String NASTY_SMALL_DOUBLE
public static int parseInt(char[] ch, int off, int len)
Note: public to let unit tests call it; not meant to be used by any code outside this package.
ch
- Buffer that contains integer value to decodeoff
- Offset of the first digit character in bufferlen
- Length of the number to decode (in characters)int
valuepublic static int parseInt(String s)
int
:
caller is expected to only calls this in cases where this can be guaranteed
(basically: number of digits does not exceed 9)
NOTE: semantics differ significantly from parseInt(char[], int, int)
.
s
- String that contains integer value to decodeint
valuepublic static long parseLong(char[] ch, int off, int len)
public static long parseLong19(char[] ch, int off, boolean negative)
It is the callers responsibility to make sure the input is exactly 19 digits.
and fits into a 64bit long by calling inLongRange(char[], int, int, boolean)
first.
Note that input String must NOT contain leading minus sign (even
if negative
is set to true).
ch
- Buffer that contains integer value to decodeoff
- Offset of the first digit character in buffernegative
- Whether original number had a minus signlong
valuepublic static long parseLong(String s)
parseInt(String)
but for long
values.s
- String that contains long
value to decodelong
valuepublic static boolean inLongRange(char[] ch, int off, int len, boolean negative)
ch
- Buffer that contains long value to checkoff
- Offset of the first digit character in bufferlen
- Length of the number to decode (in characters)negative
- Whether original number had a minus sign (which is
NOT passed to this method) or notTrue
if specified String representation is within Java
long
range; false
if not.public static boolean inLongRange(String s, boolean negative)
inLongRange(char[],int,int,boolean)
, but
with String arguments
- String that contains long
value to checknegative
- Whether original number had a minus sign (which is
NOT passed to this method) or notTrue
if specified String representation is within Java
long
range; false
if not.public static int parseAsInt(String s, int def)
public static long parseAsLong(String s, long def)
public static double parseAsDouble(String s, double def)
s
- a string representing a number to parsedef
- the default to return if `s` is not a parseable numberparseAsDouble(String, double, boolean)
public static double parseAsDouble(String s, double def, boolean useFastParser)
s
- a string representing a number to parsedef
- the default to return if `s` is not a parseable numberuseFastParser
- whether to use FastDoubleParser
@Deprecated public static double parseDouble(String s) throws NumberFormatException
parseDouble(String, boolean)
insteads
- a string representing a number to parseNumberFormatException
- if string cannot be represented by a double where useFastParser=falseparseDouble(String, boolean)
public static double parseDouble(String s, boolean useFastParser) throws NumberFormatException
s
- a string representing a number to parseuseFastParser
- whether to use FastDoubleParser
NumberFormatException
- if string cannot be represented by a double@Deprecated public static float parseFloat(String s) throws NumberFormatException
parseFloat(String, boolean)
insteads
- a string representing a number to parseNumberFormatException
- if string cannot be represented by a float where useFastParser=falseparseFloat(String, boolean)
public static float parseFloat(String s, boolean useFastParser) throws NumberFormatException
s
- a string representing a number to parseuseFastParser
- whether to use FastDoubleParser
NumberFormatException
- if string cannot be represented by a float@Deprecated public static BigDecimal parseBigDecimal(String s) throws NumberFormatException
parseBigDecimal(String, boolean)
insteads
- a string representing a number to parseNumberFormatException
- if the char array cannot be represented by a BigDecimalpublic static BigDecimal parseBigDecimal(String s, boolean useFastParser) throws NumberFormatException
s
- a string representing a number to parseuseFastParser
- whether to use custom fast parser (true) or JDK default (false) parserNumberFormatException
- if the char array cannot be represented by a BigDecimal@Deprecated public static BigDecimal parseBigDecimal(char[] ch, int off, int len) throws NumberFormatException
parseBigDecimal(char[], int, int, boolean)
insteadch
- a char array with text that makes up a numberoff
- the offset to apply when parsing the number in the char arraylen
- the length of the number in the char arrayNumberFormatException
- if the char array cannot be represented by a BigDecimalpublic static BigDecimal parseBigDecimal(char[] ch, int off, int len, boolean useFastParser) throws NumberFormatException
ch
- a char array with text that makes up a numberoff
- the offset to apply when parsing the number in the char arraylen
- the length of the number in the char arrayuseFastParser
- whether to use custom fast parser (true) or JDK default (false) parserNumberFormatException
- if the char array cannot be represented by a BigDecimal@Deprecated public static BigDecimal parseBigDecimal(char[] ch) throws NumberFormatException
parseBigDecimal(char[], boolean)
insteadch
- a char array with text that makes up a numberNumberFormatException
- if the char array cannot be represented by a BigDecimalpublic static BigDecimal parseBigDecimal(char[] ch, boolean useFastParser) throws NumberFormatException
ch
- a char array with text that makes up a numberuseFastParser
- whether to use custom fast parser (true) or JDK default (false) parserNumberFormatException
- if the char array cannot be represented by a BigDecimal@Deprecated public static BigInteger parseBigInteger(String s) throws NumberFormatException
parseBigInteger(String, boolean)
insteads
- a string representing a number to parseNumberFormatException
- if string cannot be represented by a BigIntegerpublic static BigInteger parseBigInteger(String s, boolean useFastParser) throws NumberFormatException
s
- a string representing a number to parseuseFastParser
- whether to use custom fast parser (true) or JDK default (false) parserNumberFormatException
- if string cannot be represented by a BigIntegerpublic static BigInteger parseBigIntegerWithRadix(String s, int radix, boolean useFastParser) throws NumberFormatException
s
- a string representing a number to parseradix
- for parseuseFastParser
- whether to use custom fast parser (true) or JDK default (false) parserNumberFormatException
- if string cannot be represented by a BigIntegerpublic static boolean looksLikeValidNumber(String s)
The differences to stricter JSON Number are:
Note: no trimming (String.trim()
) nor null checks are performed
on String passed.
Note: this method returning true
DOES NOT GUARANTEE String is valid
number but just that it looks close enough.
s
- String to validateCopyright © 2008–2024 FasterXML. All rights reserved.