NumberUtil { public static double parseDouble(byte[] bytes, int start, int length) { if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0) { throw new NumberFormatException("Empty byte array!"); } int offset = start; int end = start + length; while (offset < end && bytes[offset] == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } boolean sign = false; if (bytes[offset] == '-') { sign = true; offset++; } else if (bytes[offset] == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } int mantSize = 0; int decicalOffset = -1; for (; offset < end; offset++) { if (!isDigit(bytes[offset])) { if ((bytes[offset] != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; } int fracExponent; if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; int exponent = 0; offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((bytes[offset] != 'E') && (bytes[offset] != 'e')) { throw new NumberFormatException(new String(bytes, start, length)); } offset++; if (bytes[offset] == '-') { expSign = true; offset++; } else if (bytes[offset] == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(bytes[offset])) { exponent = exponent * 10 + (bytes[offset] - '0'); } else { throw new NumberFormatException(new String(bytes, start, length)); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(new String(bytes, start, length)); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }  static long unsigned32(int n); static int unsigned16(short n); static byte[] toAsciiBytes(Number i); static byte[] toAsciiBytes(short i); static byte[] toAsciiBytes(int i); static byte[] toAsciiBytes(long i); static byte[] toAsciiBytes(float i); static byte[] toAsciiBytes(double i); static double parseDouble(byte[] bytes, int start, int length); static int parseInt(byte[] bytes, int start, int length); static int parseInt(byte[] bytes, int start, int length, int radix); static long parseLong(byte[] bytes, int start, int length); static long parseLong(byte[] bytes, int start, int length, int radix); static void writeUTF8(OutputStream out, int i); static void writeUTF8(OutputStream out, long i); static double parseDouble(ByteBuf bytes); static double parseDouble(ByteBuf bytes, int start, int length); static int parseInt(ByteBuf bytes); static int parseInt(ByteBuf bytes, int start, int length); static int parseInt(ByteBuf bytes, int start, int length, int radix); static long parseLong(ByteBuf bytes); static long parseLong(ByteBuf bytes, int start, int length); static long parseLong(ByteBuf bytes, int start, int length, int radix); static Number numberValue(Class<?> numberClazz, String value); static long mergeToLong(int value1, int value2); static int toHighInt(long value); static int toLowInt(long value); static final double[] powersOf10; }
@Test public void testParseDouble() { double double1 = 2.0015E7; byte[] bytes1 = Double.toString(double1).getBytes(); assertEquals(double1, NumberUtil.parseDouble(bytes1, 0, bytes1.length), 0.0); double double2 = 1.345E-7; byte[] bytes2 = Double.toString(double2).getBytes(); assertEquals(double2, NumberUtil.parseDouble(bytes2, 0, bytes2.length), 0.0); double double3 = -1.345E-7; byte[] bytes3 = Double.toString(double3).getBytes(); assertEquals(double3, NumberUtil.parseDouble(bytes3, 0, bytes3.length), 0.0); double double4 = 4; byte[] bytes4 = Double.toString(double4).getBytes(); assertEquals(double4, NumberUtil.parseDouble(bytes4, 0, bytes4.length), 0.0); byte[] bytes5 = "0123456789.012345E012345".getBytes(); assertEquals(6789.012345E01, NumberUtil.parseDouble(bytes5, 6, 14), 0.0); }