public class NumberParser extends Object
10 million meters
7 inches
2.3 sq ft
12:30 pm
100 km
12 ml
10 Mb
Example:
System.out.println(NumberParser.normalize("It was 1.2 inches long"));
--> "It was 0.030479999999999997#meter long"
System.out.println(toLong("more than ten kB"));
--> 10000.0
| Modifier and Type | Field and Description |
|---|---|
static String |
FLOAT
A number as a capturing RegEx
|
static String |
INT
An integer as a capturing regex
|
static Pattern |
NUMBERPATTERN
The number pattern
|
static String |
POSINT
An integer as a capturing regex
|
static Map<String,Double> |
prefixes
Maps decimal prefixes (like "giga") to their double
|
| Constructor and Description |
|---|
NumberParser() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
different(String n1,
String n2)
TRUE if the numbers differ in the unit and or by more than 10%
|
static Double |
getDouble(CharSequence d)
Converts a String that contains a (non-normalized) number to a double or
null
|
static Integer |
getInt(CharSequence d)
Converts a String that contains a (non-normalized) number to a int or
null
|
static Long |
getLong(CharSequence d)
Converts a String that contains a (non-normalized) number to a long or
null
|
static String |
getNumber(CharSequence d)
Extracts the pure number from a String containing a normalized number,
else null
|
static String[] |
getNumberAndUnit(CharSequence d,
int[] pos)
Extracts the number and its unit from a String containing a normalized
number, else null, returns start and end position in pos[0] and pos[1]
resp.
|
static List<String> |
getNumbers(CharSequence d)
Extracts the numbers and units from a normalized String
|
static boolean |
isFloat(String s)
Tells whether this string is a normalized number
|
static boolean |
isInt(String s)
Tells whether this string is a normalized integer number
|
static boolean |
isNonNegativeInt(String s)
Tells whether this string is a normalized non-negative integer number
|
static boolean |
isNumberAndUnit(String s)
Tells whether this string is a normalized number with unit
|
static void |
main(String[] argv)
Test method
|
static String |
newNumber(double d,
String type)
Creates a normalized number from a double and a type.
|
static String |
newNumber(String n)
Creates a normalized number without a type.
|
static String |
newNumber(String n,
String type)
Creates a normalized number from a number and a type
|
static String |
normalize(CharSequence s)
Normalizes all numbers in a String
Note: If you fix something in this version,
please try to apply the same fix at
the position change tracking function below
|
static String |
normalize(CharSequence s,
PositionTracker posTracker)
Normalizes all numbers in a String and updates a position mapping with the introduced pos changes
Note: If you fix something in this version,
please try to apply the same fix at
the non-tracking function above
|
static Double |
parseDouble(String d)
Calls Double.parseDouble, returns an Integer or NULL
|
static Integer |
parseInt(String d)
Calls Integer.parseInt, returns an Integer or NULL
|
public static final Map<String,Double> prefixes
public static final String FLOAT
public static final String POSINT
public static final String INT
public static final Pattern NUMBERPATTERN
public static final String newNumber(String n, String type)
public static final String newNumber(String n)
newNumber(String n, String type)public static final String newNumber(double d, String type)
newNumber(String n, String type)public static boolean isFloat(String s)
public static boolean isInt(String s)
public static boolean isNonNegativeInt(String s)
public static boolean isNumberAndUnit(String s)
public static String normalize(CharSequence s)
public static String normalize(CharSequence s, PositionTracker posTracker)
public static String getNumber(CharSequence d)
public static String[] getNumberAndUnit(CharSequence d, int[] pos)
public static List<String> getNumbers(CharSequence d)
public static Double getDouble(CharSequence d)
public static Long getLong(CharSequence d)
public static Integer getInt(CharSequence d)
public static Integer parseInt(String d)
public static Double parseDouble(String d)
public static boolean different(String n1, String n2)
Copyright © 2018. All rights reserved.