private static String convertStringToExponentForm()

in SdmxEdiParser/src/main/java/org/sdmxsource/sdmx/ediparser/util/EDIUtil.java [331:435]


    private static String convertStringToExponentForm(String aStr) {
        double aDouble = Double.valueOf(aStr);

        // Early out in the corner-case of lots of zeroes
        if (aDouble == 0) {
            return "0";
        }

        double intPart = (long) aDouble;

        // If we start with a zero, format it like so
        if (intPart == 0 || aDouble < 0.00001) {
            NumberFormat formatter;
            if (aDouble > 0) {
                if (!aStr.contains("E")) {
                    // Original string does NOT contain an exponent, but need to check the double of the string since this may have one.
                    aStr = Double.toString(aDouble);
                    // Does this new string contain an exponent?
                    if (!aStr.contains("E")) {
                        formatter = TEN_HASH_FORMATTER;
                    } else {
                        int ePos = aStr.indexOf("E");
                        if (aStr.length() - ePos == 4) {
                            formatter = NINE_HASH_FORMATTER;
                        } else {
                            formatter = TEN_HASH_FORMATTER;
                        }
                    }
                } else {
                    // There is an Exponent
                    // Convert the original String to the Double form. This is because the double form may have changed
                    // the exponent length. e.g. from E9 to E10
                    aStr = Double.toString(aDouble);
                    int ePos = aStr.indexOf("E");
                    if (aStr.length() - ePos == 4) {
                        formatter = NINE_HASH_FORMATTER;
                    } else {
                        formatter = TEN_HASH_FORMATTER;
                    }
                }
            } else {
                // We lose one digit for the leading negative symbol
                // But if the E is a negative number we need to drop another digit
                aStr = Double.toString(aDouble);
                int ePos = aStr.indexOf("E");
                if (aStr.contains("E-")) {
                    // It's a positive E
                    if (aStr.length() - ePos == 3) {
                        formatter = NINE_HASH_FORMATTER;
                    } else {
                        formatter = EIGHT_HASH_FORMATTER;
                    }
                } else {
                    // It's a negative E so does it have 1 or 2 values after the E
                    if (aStr.length() - ePos == 4) {
                        formatter = EIGHT_HASH_FORMATTER;
                    } else {
                        formatter = NINE_HASH_FORMATTER;
                    }
                }
            }
            String convertedValue = formatter.format(aDouble);
            return convertedValue;
        }

        int intPartLength;
        if (intPart > 0) {
            String numberAsString = aStr;
            int dotIndex = numberAsString.indexOf(".");
            if (dotIndex != -1) {
                intPartLength = dotIndex;
            } else {
                intPartLength = numberAsString.length();
            }
            // If the part before the dot is longer than 15 characters, we just round to the exponent
            if (intPartLength > 15) {
                NumberFormat formatter = new DecimalFormat("#.##########E00");  // 10 hashes, 2 char exponent
                return formatter.format(aDouble);
            }
        } else {
            intPartLength = 0;
        }

        // Either round to 15 characters or 14 characters (if negative)
        BigDecimal bd = new BigDecimal(aDouble);
        if (aDouble > 0) {
            bd = bd.setScale(14 - intPartLength, RoundingMode.HALF_UP);
        } else {
            // It's a negative
            bd = bd.setScale(13 - intPartLength, RoundingMode.HALF_UP);
        }

        // If the number is still bigger than 15 characters
        if (bd.toString().length() > 15) {
            NumberFormat formatter;
            if (aDouble > 0) {
                formatter = new DecimalFormat("#.##########E00");  // 10 Hashes, 2 char exponent
            } else {
                formatter = new DecimalFormat("#.#########E00");   // 9 Hashes, 2 char exponent
            }
            return formatter.format(aDouble);
        }

        return bd.toString();
    }