/** * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * * @param c Complex object to format. * @return A formatted number in the form "Re(c) + Im(c)i". */ public String format(Complex c) { return format(c, new StringBuffer(), new FieldPosition(0)).toString(); }
/** * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * * @param c Double object to format. * @return A formatted number. */ public String format(Double c) { return format(new Complex(c, 0), new StringBuffer(), new FieldPosition(0)).toString(); }
/** * Formats a object to produce a string. {@code obj} must be either a * {@link Complex} object or a {@link Number} object. Any other type of * object will result in an {@link IllegalArgumentException} being thrown. * * @param obj the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @throws MathIllegalArgumentException is {@code obj} is not a valid type. */ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) throws MathIllegalArgumentException { StringBuffer ret = null; if (obj instanceof Complex) { ret = format( (Complex)obj, toAppendTo, pos); } else if (obj instanceof Number) { ret = format(new Complex(((Number)obj).doubleValue(), 0.0), toAppendTo, pos); } else { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_INSTANCE_AS_COMPLEX, obj.getClass().getName()); } return ret; }
/** * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * * @param c Complex object to format. * @return A formatted number in the form "Re(c) + Im(c)i". */ public String format(Complex c) { return format(c, new StringBuffer(), new FieldPosition(0)).toString(); }
/** * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * * @param c Double object to format. * @return A formatted number. */ public String format(Double c) { return format(new Complex(c, 0), new StringBuffer(), new FieldPosition(0)).toString(); }
/** * Formats a object to produce a string. {@code obj} must be either a * {@link Complex} object or a {@link Number} object. Any other type of * object will result in an {@link IllegalArgumentException} being thrown. * * @param obj the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @throws MathIllegalArgumentException is {@code obj} is not a valid type. */ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) throws MathIllegalArgumentException { StringBuffer ret = null; if (obj instanceof Complex) { ret = format( (Complex)obj, toAppendTo, pos); } else if (obj instanceof Number) { ret = format(new Complex(((Number)obj).doubleValue(), 0.0), toAppendTo, pos); } else { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_INSTANCE_AS_COMPLEX, obj.getClass().getName()); } return ret; }
ComplexFormat cf = new ComplexFormat(); Complex c = cf.parse("1.110 + 2.222i"); Complex second = new Complex(2.0, 5.0); Complex answer = c.log(); // natural logarithm. answer = c.cos(); // cosine answer = c.pow(second); // first raised to the power of second ComplexFormat format = new ComplexFormat(); // default format String s1 = format.format(c); // s1 contains "1.110 + 2.222i" String s2 = format.format(second); // s2 contains "2.0 + 5.0i"