I found a lot of information about converting the original byte information into a readable format, but I need to do the opposite, that is, convert the string “1.6 GB” to a long value 1717990000. Is there a built-in / well-defined way to do this, or will I pretty much have to roll on your own?
[Edit]: Here is my first hit ...
static class ByteFormat extends NumberFormat { @Override public StringBuffer format(double arg0, StringBuffer arg1, FieldPosition arg2) { // TODO Auto-generated method stub return null; } @Override public StringBuffer format(long arg0, StringBuffer arg1, FieldPosition arg2) { // TODO Auto-generated method stub return null; } @Override public Number parse(String arg0, ParsePosition arg1) { return parse (arg0); } @Override public Number parse(String arg0) { int spaceNdx = arg0.indexOf(" "); double ret = Double.parseDouble(arg0.substring(0, spaceNdx)); String unit = arg0.substring(spaceNdx + 1); int factor = 0; if (unit.equals("GB")) { factor = 1073741824; } else if (unit.equals("MB")) { factor = 1048576; } else if (unit.equals("KB")) { factor = 1024; } return ret * factor; } }
java long-integer byte human-readable
Dan forbes
source share