Java Char Array to Double charArrayToDouble(char[] data, int start, int length, int[] end)

Here you can find the source of charArrayToDouble(char[] data, int start, int length, int[] end)

Description

char Array To Double

License

Open Source License

Declaration

public static double charArrayToDouble(char[] data, int start, int length, int[] end) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double charArrayToDouble(char[] data, int start, int length, int[] end) {
        boolean neg = false;
        if (data[start] == '-') {
            neg = true;/*from  ww  w. ja  v  a2s  .c  o  m*/
            start++;
        } else if (data[start] == '+') {
            start++;
        }

        long value = 0;
        int i;
        int dotIndex = -1;
        int powerMod = 0;
        for (i = start; i < length; i++) {
            final char ch = data[i];
            if (ch >= '0' && ch <= '9') {
                powerMod++;
                value = value * 10 + (ch - '0');
            } else if (ch == '.' && dotIndex < 0) {
                dotIndex = i;
            } else if (ch == 'e' || ch == 'E') {
                powerMod -= charArrayToInt(data, i + 1, length, end);
                break;
            } else {
                break;
            }
        }
        end[0] = i;
        if (dotIndex < 0) {
            dotIndex = i;
        }
        powerMod -= dotIndex - start;
        double result = value / Math.pow(10, powerMod);
        return neg ? -result : result;
    }

    public static int charArrayToInt(char[] data, int start, int length, int[] end) throws NumberFormatException {
        boolean neg = false;
        if (data[start] == '-') {
            neg = true;
            start++;
        } else if (data[start] == '+') {
            start++;
        }
        int result = 0;
        int i;
        for (i = start; i < length; i++) {
            int digit = data[i] - '0';
            if (digit < 0 || digit > 9) {
                return result;
            }
            result = result * 10 + digit;
        }
        end[0] = i;
        return neg ? -result : result;
    }
}