United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4984872 (jsr 13) BigDecimal needs toString method without exponents
JDK-4984872 : (jsr 13) BigDecimal needs toString method without exponents

Details
Type:
Bug
Submit Date:
2004-01-28
Status:
Resolved
Updated Date:
2004-02-20
Project Name:
JDK
Resolved Date:
2004-02-20
Component:
core-libs
OS:
generic
Sub-Component:
java.math
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0 (b40)

Related Reports
Relates:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

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

A fine idea.

###@###.### 2004-01-28
                                     
2004-01-28
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
                                     
2004-02-09
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger-beta2

FIXED IN:
tiger-beta2

INTEGRATED IN:
tiger-b40
tiger-beta2


                                     
2004-10-02
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.
                                     
2006-07-25



Hardware and Software, Engineered to Work Together