Ejemplo n.º 1
0
  /** flo_to_s */
  @JRubyMethod(name = "to_s")
  @Override
  public IRubyObject to_s() {
    Ruby runtime = getRuntime();
    if (Double.isInfinite(value))
      return RubyString.newString(runtime, value < 0 ? "-Infinity" : "Infinity");
    if (Double.isNaN(value)) return RubyString.newString(runtime, "NaN");

    ByteList buf = new ByteList();
    // Under 1.9, use full-precision float formatting (JRUBY-4846).
    // Double-precision can represent around 16 decimal digits;
    // we use 20 to ensure full representation.
    Sprintf.sprintf(buf, Locale.US, "%#.20g", this);
    int e = buf.indexOf('e');
    if (e == -1) e = buf.getRealSize();
    ASCIIEncoding ascii = ASCIIEncoding.INSTANCE;

    if (!ascii.isDigit(buf.get(e - 1))) {
      buf.setRealSize(0);
      Sprintf.sprintf(buf, Locale.US, "%#.14e", this);
      e = buf.indexOf('e');
      if (e == -1) e = buf.getRealSize();
    }

    int p = e;
    while (buf.get(p - 1) == '0' && ascii.isDigit(buf.get(p - 2))) p--;
    System.arraycopy(buf.getUnsafeBytes(), e, buf.getUnsafeBytes(), p, buf.getRealSize() - e);
    buf.setRealSize(p + buf.getRealSize() - e);

    buf.setEncoding(USASCIIEncoding.INSTANCE);

    return runtime.newString(buf);
  }
Ejemplo n.º 2
0
  private ByteList marshalDump() {
    if (Double.isInfinite(value)) return value < 0 ? NEGATIVE_INFINITY_BYTELIST : INFINITY_BYTELIST;
    if (Double.isNaN(value)) return NAN_BYTELIST;

    ByteList byteList = new ByteList();
    // Always use US locale, to ensure "." separator. JRUBY-5918
    Sprintf.sprintf(byteList, Locale.US, "%.17g", RubyArray.newArray(getRuntime(), this));
    return byteList;
  }