From 03f66650d37ea5ca5b84d23312585bb3394fecdf Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Thu, 14 Jul 2011 15:25:11 +0000 Subject: [PATCH] MATH-617 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 --- .../commons/math/complex/ComplexFormat.java | 37 ++++++++++++++++++- .../complex/ComplexFormatAbstractTest.java | 32 ++++++++++++++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/math/complex/ComplexFormat.java b/src/main/java/org/apache/commons/math/complex/ComplexFormat.java index f64c38737..f3d56096d 100644 --- a/src/main/java/org/apache/commons/math/complex/ComplexFormat.java +++ b/src/main/java/org/apache/commons/math/complex/ComplexFormat.java @@ -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 diff --git a/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java b/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java index f68561e57..b9e3b0ebe 100644 --- a/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java +++ b/src/test/java/org/apache/commons/math/complex/ComplexFormatAbstractTest.java @@ -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); }