JDK-6354947 : [Fmt-*] Clarify DecimalFormat description of FieldPosition use
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.1.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-11-23
  • Updated: 2017-12-18
  • Resolved: 2017-11-30
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.
JDK 10
10 b34Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06)
Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)

FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
  From http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html:

public StringBuffer format(long number,
                           StringBuffer result,
                           FieldPosition fieldPosition)
Format a long to produce a string.
Specified by:
format in class NumberFormat

Parameters:
number - The long to format
result - where the text is to be appended
fieldPosition - On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns:
The formatted number string

In spite of what the documentation says about fieldPosition, the alignment field seems to be ignored.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
(See test case)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
B,E 0,0
B,E 10,23
          1,234,567,890
B,E 10,23
ACTUAL -
B,E 0,0
B,E 10,23
1,234,567,890
B,E 0,13


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
public class TestFormat{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
DecimalFormat df = new DecimalFormat();
FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
System.out.println("B,E "+fp.getBeginIndex()+","+fp.getEndIndex());//B,E 0,0
fp.setBeginIndex(10); fp.setEndIndex(23);
System.out.println("B,E "+fp.getBeginIndex()+","+fp.getEndIndex());//B,E 10,23
df.format(1234567890L,sb,fp);// Use FieldPosition to affect output
System.out.println(sb); //1,234,567,890 - no leading spaces
System.out.println("B,E "+fp.getBeginIndex()+","+fp.getEndIndex());//B,E 0,13
}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
[Ignore the documentation!]

Comments
Partial fix is attached.
08-11-2016

I realized that java.text.Format and its subclasses require more intensive documentation work. Perhaps there should be an umbrella bug report to cover all Format documentation issues, such as JDK-6235363.
25-09-2013

The FieldPosition argument description is just confusing. The pos argument specifies the field whose position information is stored upon return. In addition to the argument description fix, some missing @throws and @return descriptions will be added.
24-09-2013

EVALUATION The on input part should be removed. MessageFormat and DateFormat have the same problem.
08-08-2011