Name: gm110360 Date: 02/27/2004
FULL PRODUCT VERSION :
java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Same as bug 4189935.
Our software needs to deal with formatting and parsing of JavaBean Properties for visual rendering and editing of nearly arbitrary data.
The user can specify a java.text.Format instance for every JavaBean Property (by configuration).
We are using that instance to call the parseObject and format methods with the provided JavaBean Property's current value. Actually this works great since both, JavaBean API also parseObject plus format are dealing with Objects so a generic solution is easy.
Unfortunately, when specifying a number to a DecimalFormat that has a single zero behind the decimal separator, parseObject doesn't anymore deliver a Double but it switches output to Long.
Sure this is API conformant, but it is nuts.
A user that utilizes a DecimalFormat (Decimal, not Number in general!) with a pattern that contains the placeholder for the decimal separator plus a placeholder for at least one fractional digit doesn't expect to sometimes a Double sometimes a Long; he expects to get a Double every time.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the example program (see below).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Should show that the parseObject delivered a java.lang.Double as it the parsed DecimalFormat contains placeholders for decimal separator plus fractional digits.
ACTUAL -
Shows that the parseObject returned a java.lang.Long instance which no one would expect.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class FormatTest {
public static void main(String[] args) throws ParseException {
final DecimalFormat f = new DecimalFormat("###.##");
final Number n1 = new Double(123.0);
final String s = f.format(n1);
final Number n2 = f.parse(s);
System.out.println(n2.getClass().getName());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Need to check for the class of the returned instance.
(Incident Review ID: 223754)
======================================================================
- relates to
-
JDK-8352755 Misconceptions about j.text.DecimalFormat digits during parsing
-
- Open
-