/**
* Parse a human-readable string possibly including SI units into an integer.
* @throws NumberFormatException
* if the string is not parseable
*/
public static int parseSIInt(String s) throws NumberFormatException {
int res = 1;
int x = s.length() - 1;
int idx;
try {
long[] l =
{
1000,
1 << 10,
1000 * 1000,
1 << 20,
1000 * 1000 * 1000,
1 << 30 };
while ((x >= 0)
&& ((idx = "kKmMgG".indexOf(s.charAt(x))) != -1)) {
x--;
res *= l[idx];
}
res *= Double.parseDouble(s.substring(0, x + 1));
} catch (ArithmeticException e) {
res = Integer.MAX_VALUE;
throw new NumberFormatException(e.getMessage());
}
return res;
}
Welcome to the bulix.org / pastebin. Please don't use this pastebin for illegal purposes, defamation or kitten-squashing.
This pastebin is written using PHP and MySQL and relies on Alex Gorbatchev's syntax hhighlighter (JavaScript based). To avoid spam, you will be required to complete a small mathematical challenge when adding a new paste.
New! Try the pastebin command-line tool: paste.py (requires Python and python-beautifulsoup).
Powered by the Bulix.org Code Pastebin, by Maxime Petazzoni. View pastebin statistics.