Пример #1
0
  @Override
  public StringBuffer format(Object value, StringBuffer buffer, FieldPosition field) {

    if (!(value instanceof Number)) {
      throw new IllegalArgumentException();
    }
    if (buffer == null || field == null) {
      throw new NullPointerException();
    }

    String fieldType = null;
    if (field != null) {
      fieldType = getFieldType(field.getFieldAttribute());
    }

    Number number = (Number) value;

    if (number instanceof BigInteger) {
      BigInteger valBigInteger = (BigInteger) number;
      String result =
          NativeDecimalFormat.format(
              this.addr, valBigInteger.toString(10), field, fieldType, null, 0);
      return buffer.append(result);
    } else if (number instanceof BigDecimal) {
      BigDecimal valBigDecimal = (BigDecimal) number;
      String result =
          NativeDecimalFormat.format(
              this.addr,
              valBigDecimal.unscaledValue().toString(10),
              field,
              fieldType,
              null,
              valBigDecimal.scale());
      return buffer.append(result);
    } else {
      double dv = number.doubleValue();
      long lv = number.longValue();
      if (dv == lv) {
        String result = NativeDecimalFormat.format(this.addr, lv, field, fieldType, null);
        return buffer.append(result);
      }
      String result = NativeDecimalFormat.format(this.addr, dv, field, fieldType, null);
      return buffer.append(result);
    }
  }
Пример #2
0
  @Override
  public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {

    if (buffer == null) {
      throw new NullPointerException();
    }

    String fieldType = null;

    if (field != null) {
      fieldType = getFieldType(field.getFieldAttribute());
    }

    String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);

    buffer.append(result.toCharArray(), 0, result.length());

    return buffer;
  }