private static String normalizeFPNumber(String value,String minMantissa,String maxMantissa,String minExponent,String maxExponent){
  value=collapseWhiteSpace(value);
  if (value.contains(" ")) {
    throwIAE("No space allowed in floating point lexical value (" + value + ")");
  }
  if (value.equals(POSITIVE_INFINITY) || value.equals(NEGATIVE_INFINITY) || value.equals(NaN)) {
    return value;
  }
  int eIdx=value.indexOf('E');
  if (eIdx == -1) {
    eIdx=value.indexOf('e');
  }
  String mantissa, exponent;
  if (eIdx == -1) {
    mantissa=normalizeDecimal(value);
    exponent="0";
  }
 else {
    mantissa=normalizeDecimal(value.substring(0,eIdx));
    exponent=normalizeInteger(value.substring(eIdx + 1));
  }
  int shift=0;
  int dotIdx=mantissa.indexOf('.');
  int digitCount=dotIdx;
  if (mantissa.charAt(0) == '-') {
    digitCount--;
  }
  if (digitCount > 1) {
    StringBuilder sb=new StringBuilder(mantissa.length());
    int firstDigitIdx=0;
    if (mantissa.charAt(0) == '-') {
      sb.append('-');
      firstDigitIdx=1;
    }
    sb.append(mantissa.charAt(firstDigitIdx));
    sb.append('.');
    sb.append(mantissa.substring(firstDigitIdx + 1,dotIdx));
    sb.append(mantissa.substring(dotIdx + 1));
    mantissa=sb.toString();
    int nonZeroIdx=mantissa.length() - 1;
    while (nonZeroIdx >= 3 && mantissa.charAt(nonZeroIdx) == '0') {
      nonZeroIdx--;
    }
    if (nonZeroIdx < 3 && mantissa.charAt(0) == '-') {
      nonZeroIdx++;
    }
    if (nonZeroIdx < mantissa.length() - 1) {
      mantissa=mantissa.substring(0,nonZeroIdx + 1);
    }
    shift=1 - digitCount;
  }
 else   if (mantissa.startsWith("0.") || mantissa.startsWith("-0.")) {
    int nonZeroIdx=2;
    boolean negative=false;
    if (mantissa.charAt(0) == '-') {
      nonZeroIdx++;
      negative=true;
    }
    while (nonZeroIdx < mantissa.length() && mantissa.charAt(nonZeroIdx) == '0') {
      nonZeroIdx++;
    }
    if (nonZeroIdx < mantissa.length()) {
      StringBuilder sb=new StringBuilder(mantissa.length());
      if (negative) {
        sb.append('-');
      }
      sb.append(mantissa.charAt(nonZeroIdx));
      sb.append('.');
      if (nonZeroIdx == mantissa.length() - 1) {
        sb.append('0');
      }
 else {
        sb.append(mantissa.substring(nonZeroIdx + 1));
      }
      mantissa=sb.toString();
      shift=negative ? nonZeroIdx - 2 : nonZeroIdx - 1;
    }
  }
  if (shift != 0) {
    try {
      int exp=Integer.parseInt(exponent);
      exponent=String.valueOf(exp - shift);
    }
 catch (    NumberFormatException e) {
      throw new RuntimeException("NumberFormatException: " + e.getMessage());
    }
  }
  if (minMantissa != null) {
    if (compareCanonicalDecimals(mantissa,minMantissa) < 0) {
      throwIAE("Mantissa smaller than minimum value (" + minMantissa + ")");
    }
  }
  if (maxMantissa != null) {
    if (compareCanonicalDecimals(mantissa,maxMantissa) > 0) {
      throwIAE("Mantissa larger than maximum value (" + maxMantissa + ")");
    }
  }
  if (minExponent != null) {
    if (compareCanonicalIntegers(exponent,minExponent) < 0) {
      throwIAE("Exponent smaller than minimum value (" + minExponent + ")");
    }
  }
  if (maxExponent != null) {
    if (compareCanonicalIntegers(exponent,maxExponent) > 0) {
      throwIAE("Exponent larger than maximum value (" + maxExponent + ")");
    }
  }
  return mantissa + "E" + exponent;
}

private static String normalizeFPNumber ( String value , String minMantissa , String maxMantissa , String minExponent , String maxExponent ) { value = collapseWhiteSpace ( value ) ; if ( value . contains ( " " ) ) { throwIAE ( "No space allowed in floating point lexical value (" + value + ")" ) ; } if ( value . equals ( POSITIVE_INFINITY ) || value . equals ( NEGATIVE_INFINITY ) || value . equals ( NaN ) ) { return value ; } int eIdx = value . indexOf ( 'E' ) ; if ( eIdx == - 1 ) { eIdx = value . indexOf ( 'e' ) ; } String mantissa , exponent ; if ( eIdx == - 1 ) { mantissa = normalizeDecimal ( value ) ; exponent = "0" ; } else { mantissa = normalizeDecimal ( value . substring ( 0 , eIdx ) ) ; exponent = normalizeInteger ( value . substring ( eIdx + 1 ) ) ; } int shift = 0 ; int dotIdx = mantissa . indexOf ( '.' ) ; int digitCount = dotIdx ; if ( mantissa . charAt ( 0 ) == '-' ) { digitCount -- ; } if ( digitCount > 1 ) { StringBuilder sb = new StringBuilder ( mantissa . length ( ) ) ; int firstDigitIdx = 0 ; if ( mantissa . charAt ( 0 ) == '-' ) { sb . append ( '-' ) ; firstDigitIdx = 1 ; } sb . append ( mantissa . charAt ( firstDigitIdx ) ) ; sb . append ( '.' ) ; sb . append ( mantissa . substring ( firstDigitIdx + 1 , dotIdx ) ) ; sb . append ( mantissa . substring ( dotIdx + 1 ) ) ; mantissa = sb . toString ( ) ; int nonZeroIdx = mantissa . length ( ) - 1 ; while ( nonZeroIdx >= 3 && mantissa . charAt ( nonZeroIdx ) == '0' ) { nonZeroIdx -- ; } if ( nonZeroIdx < 3 && mantissa . charAt ( 0 ) == '-' ) { nonZeroIdx ++ ; } if ( nonZeroIdx < mantissa . length ( ) - 1 ) { mantissa = mantissa . substring ( 0 , nonZeroIdx + 1 ) ; } shift = 1 - digitCount ; } else if ( mantissa . startsWith ( "0." ) || mantissa . startsWith ( "-0." ) ) { int nonZeroIdx = 2 ; boolean negative = false ; if ( mantissa . charAt ( 0 ) == '-' ) { nonZeroIdx ++ ; negative = true ; } while ( nonZeroIdx < mantissa . length ( ) && mantissa . charAt ( nonZeroIdx ) == '0' ) { nonZeroIdx ++ ; } if ( nonZeroIdx < mantissa . length ( ) ) { StringBuilder sb = new StringBuilder ( mantissa . length ( ) ) ; if ( negative ) { sb . append ( '-' ) ; } sb . append ( mantissa . charAt ( nonZeroIdx ) ) ; sb . append ( '.' ) ; if ( nonZeroIdx == mantissa . length ( ) - 1 ) { sb . append ( '0' ) ; } else { sb . append ( mantissa . substring ( nonZeroIdx + 1 ) ) ; } mantissa = sb . toString ( ) ; shift = negative ? nonZeroIdx - 2 : nonZeroIdx - 1 ; } } if ( shift != 0 ) { try { int exp = Integer . parseInt ( exponent ) ; exponent = String . valueOf ( exp - shift ) ; } catch ( NumberFormatException e ) { throw new RuntimeException ( "NumberFormatException: " + e . getMessage ( ) ) ; } } if ( minMantissa != null ) { if ( compareCanonicalDecimals ( mantissa , minMantissa ) < 0 ) { throwIAE ( "Mantissa smaller than minimum value (" + minMantissa + ")" ) ; } } if ( maxMantissa != null ) { if ( compareCanonicalDecimals ( mantissa , maxMantissa ) > 0 ) { throwIAE ( "Mantissa larger than maximum value (" + maxMantissa + ")" ) ; } } if ( minExponent != null ) { if ( compareCanonicalIntegers ( exponent , minExponent ) < 0 ) { throwIAE ( "Exponent smaller than minimum value (" + minExponent + ")" ) ; } } if ( maxExponent != null ) { if ( compareCanonicalIntegers ( exponent , maxExponent ) > 0 ) { throwIAE ( "Exponent larger than maximum value (" + maxExponent + ")" ) ; } } return mantissa + "E" + exponent ; }
normalizes a floating point lexical value to its canonical representation .
