JDK-4984872 : (jsr 13) BigDecimal needs toString method without exponents
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2004-01-28
  • Updated: 2017-05-16
  • Resolved: 2004-02-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b40Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
In 1.4.x and earlier releases, the BigDecimal toString method did not use scientific notation.  The scale of a fractional value was a function of the number of leading zeros.  With jsr 13 and the introduction of scales of both signs, the use of scientific notation is necessary for the toString method to preserve both numerical value and representation information.  However, there remains a need to generated old-style strings; e.g. interfacing with data bases.

Comments
EVALUATION Responding to JDC comments, this issue was addressed by adding the BigDecimal.toPlainString method, which has the pre-Tiger behavior of not using scientific notation.
25-07-2006

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta2 FIXED IN: tiger-beta2 INTEGRATED IN: tiger-b40 tiger-beta2
02-10-2004

SUGGESTED FIX src/share/classes/java/math>sccs sccsdiff -r1.50 -r1.51 BigDecimal.java ------- BigDecimal.java ------- 137c137 < * rounding mode, br> --- > * rounding mode, <br> 1442c1442 < * @throws ArithmeticException if <tt>mc.precision</tt> > 0 and the result --- > * @throws ArithmeticException if <tt>mc.precision</tt> &gt; 0 and the result 2415a2416,2480 > * Returns a string representation of this <tt>BigDecimal</tt> > * without an exponent field. For values with a positive scale, > * the number of digits to the right of the decimal point is used > * to indicate scale. For values with a zero or negative scale, > * the resulting string is generated as if the value were > * converted to a numerically equal value with zero scale and as > * if all the trailing zeros of the zero scale value were present > * in the result. > * > * The entire string is prefixed by a minus sign character '-' > * (<tt>'&#92;u002D'</tt>) if the unscaled value is less than > * zero. No sign character is prefixed if the unscaled value is > * zero or positive. > * > * Note that if the result of this method is passed to the > * {@linkplain #BigDecimal(String) string constructor}, only the > * numerical value of this <tt>BigDecimal</tt> will necessarily be > * recovered; the representation of the new <tt>BigDecimal</tt> > * may have a different scale. In particular, if this > * <tt>BigDecimal</tt> has a positive scale, the string resulting > * from this method will have a scale of zero when processed by > * the string constructor. > * > * (This method behaves analogously to the <tt>toString</tt> > * method in 1.4 and earlier releases.) > * > * @return a string representation of this <tt>BigDecimal</tt> > * without an exponent field. > * @since 1.5 > * @see #toString() > * @see #toEngineeringString() > */ > public String toPlainString() { > BigDecimal bd = this; > if (bd.scale < 0) > bd = bd.setScale(0); > if (bd.scale == 0) /* No decimal point */ > return bd.intVal.toString(); > return bd.getValueString(bd.signum(), bd.intVal.abs().toString(), bd.scale); > } > > /* Returns a digit.digit string */ > private String getValueString(int signum, String intString, int scale) { > /* Insert decimal point */ > StringBuilder buf; > int insertionPoint = intString.length() - scale; > if (insertionPoint == 0) { /* Point goes right before intVal */ > return (signum<0 ? "-0." : "0.") + intString; > } else if (insertionPoint > 0) { /* Point goes inside intVal */ > buf = new StringBuilder(intString); > buf.insert(insertionPoint, '.'); > if (signum < 0) > buf.insert(0, '-'); > } else { /* We must insert zeros between point and intVal */ > buf = new StringBuilder(3-insertionPoint + intString.length()); > buf.append(signum<0 ? "-0." : "0."); > for (int i=0; i<-insertionPoint; i++) > buf.append('0'); > buf.append(intString); > } > return buf.toString(); > } > > > /** ###@###.### 2004-02-09
09-02-2004

EVALUATION A fine idea. ###@###.### 2004-01-28
28-01-2004