Пример #1
0
 /**
  * Converts a string representation of the data to a floating point value, based on the unitcode.
  */
 public static float string2float(String value, char unitcode) throws NumberFormatException {
   return string2float(value, SymbolInfo.ddfuc2bb(unitcode));
 }
Пример #2
0
  /*
   * Convert a float to a DDF string.
   *
   * TODO Possible optimizations here.
   */
  public static final synchronized String float2string(
      float value, final char basecode, final int target, final boolean special64) {

    final int unit = SymbolInfo.ddfuc2bb(basecode);

    if (unit >= 0) {
      if (target == PURE_DECIMAL) NF.setGroupingUsed(false);
      else NF.setGroupingUsed(true);

      NF.setMinimumFractionDigits(unit);
      NF.setMaximumFractionDigits(unit);

      String s = NF.format(value);

      if (target == INTEGER) {
        char ca[] = s.toCharArray();
        int st = 0;

        s = "";

        for (int i = 0; i < ca.length; i++) {
          if ((i == 0) && (ca[i] == '+') || (ca[i] == '-')) // A
            // leading
            // sign
            st++;
          else if ((ca[i] == '0') || (ca[i] == '.')) // Strip all
            // zeros from
            // the left
            st++;
          else if (Character.isDigit(ca[i])) break;
        }

        for (int i = st; i < ca.length; i++) {
          if (Character.isDigit(ca[i])) s = s + ca[i];
        }

        if ((value == 0.0f) && (s.length() < 1)) s = "0";

        if (value < 0) s = "-" + s;

        return s;
      } else return s;
    }

    if ((target == DECIMAL) || (target == PURE_DECIMAL)) {
      if (target == PURE_DECIMAL) NF.setGroupingUsed(false);
      else NF.setGroupingUsed(true);

      switch (unit) {
        case -1:
          NF.setMinimumFractionDigits(3);
          NF.setMaximumFractionDigits(3);
          break;
        case -2:
          NF.setMinimumFractionDigits(4);
          NF.setMaximumFractionDigits(4);
          break;
        case -3:
          NF.setMinimumFractionDigits(5);
          NF.setMaximumFractionDigits(5);
          break;
        case -4:
          NF.setMinimumFractionDigits(6);
          NF.setMaximumFractionDigits(6);
          break;
        case -5:
          NF.setMinimumFractionDigits(7);
          NF.setMaximumFractionDigits(7);
          break;
        case -6:
          NF.setMinimumFractionDigits(8);
          NF.setMaximumFractionDigits(8);
          break;
      }

      return (NF.format(value));
    }

    int sign = 1;

    if (value < 0.0f) sign = -1;

    value = Math.abs(value);

    int iWhole = (int) value;

    String sw = "" + iWhole;

    if (sign == -1) sw = "-" + sw;

    String sf = "";

    float div = value - (int) value;
    int digits = 0;

    if (unit == -1) {
      div = div * 8;
      digits = 1;
    } else if (unit == -2) {
      div = div * 16;
      digits = 2;
    } else if (unit == -3) {
      div = div * 32;
      digits = 2;
    } else if (unit == -4) {
      if (special64) {
        // 64ths --> 32nds and halves
        div = div * 320;
        digits = 3;
      } else {
        div = div * 64;
        digits = 2;
      }
    } else if (unit == -5) {
      if (special64) {
        div = div * 320;
        digits = 3;
      } else {
        div = div * 128;
        digits = 3;
      }
    } else if (unit == -6) {
      div = div * 256;
      digits = 3;
    } else {
      div = 0;
      digits = 1;
    }

    sf = "00000000" + (int) div;
    sf = sf.substring(sf.length() - digits, sf.length());

    if (target == INTEGER) {
      if (iWhole != 0) return (sw + sf);
      else {
        char c = sw.charAt(0);
        if (c == '-') return c + sf;
        else return sf;
      }
    } else return sw + "-" + sf;
  }