Make "1 + 1i" appear as "1 + i" on formatted output.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1146755 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-07-14 15:25:11 +00:00
parent b8ed51d4a0
commit 03f66650d3
2 changed files with 64 additions and 5 deletions

View File

@ -26,6 +26,7 @@ import org.apache.commons.math.util.CompositeFormat;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.MathParseException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.MathInternalError;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NoDataException;
@ -178,19 +179,51 @@ public class ComplexFormat {
// format sign and imaginary
double im = complex.getImaginary();
StringBuffer imAppendTo = new StringBuffer();
if (im < 0.0) {
toAppendTo.append(" - ");
CompositeFormat.formatDouble(-im, getImaginaryFormat(), toAppendTo, pos);
imAppendTo = formatImaginary(-im, new StringBuffer(), pos);
toAppendTo.append(imAppendTo);
toAppendTo.append(getImaginaryCharacter());
} else if (im > 0.0 || Double.isNaN(im)) {
toAppendTo.append(" + ");
CompositeFormat.formatDouble(im, getImaginaryFormat(), toAppendTo, pos);
imAppendTo = formatImaginary(im, new StringBuffer(), pos);
toAppendTo.append(imAppendTo);
toAppendTo.append(getImaginaryCharacter());
}
return toAppendTo;
}
/**
* Format the absolute value of the imaginary part.
*
* @param absIm Absolute value of the imaginary part of a complex number.
* @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.
* @throws MathInternalError if {@code absIm} is not positive.
*/
private StringBuffer formatImaginary(double absIm,
StringBuffer toAppendTo,
FieldPosition pos) {
if (absIm < 0) {
throw new MathInternalError();
}
pos.setBeginIndex(0);
pos.setEndIndex(0);
CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos);
if (toAppendTo.toString().equals("1")) {
// Remove the character "1" if it is the only one.
toAppendTo.setLength(0);
}
return toAppendTo;
}
/**
* 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

View File

@ -42,12 +42,38 @@ public abstract class ComplexFormatAbstractTest {
@Test
public void testSimpleNoDecimals() {
Complex c = new Complex(1, 1);
String expected = "1 + 1i";
Complex c = new Complex(1, 2);
String expected = "1 + 2i";
String actual = complexFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testTrimOneImaginary() {
final ComplexFormat fmt = ComplexFormat.getInstance(getLocale());
fmt.getImaginaryFormat().setMaximumFractionDigits(1);
Complex c = new Complex(1, 1.04);
String expected = "1 + i";
String actual = fmt.format(c);
Assert.assertEquals(expected, actual);
c = new Complex(1, 1.09);
expected = "1 + 1" + getDecimalCharacter() + "1i";
actual = fmt.format(c);
Assert.assertEquals(expected, actual);
c = new Complex(1, -1.09);
expected = "1 - 1" + getDecimalCharacter() + "1i";
actual = fmt.format(c);
Assert.assertEquals(expected, actual);
c = new Complex(1, -1.04);
expected = "1 - i";
actual = fmt.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testSimpleWithDecimals() {
Complex c = new Complex(1.23, 1.43);
@ -107,7 +133,7 @@ public abstract class ComplexFormatAbstractTest {
@Test
public void testDifferentImaginaryChar() {
Complex c = new Complex(1, 1);
String expected = "1 + 1j";
String expected = "1 + j";
String actual = complexFormatJ.format(c);
Assert.assertEquals(expected, actual);
}