Mail from pbk:
It's worse than that. In addition to throwing exceptions where it
shouldn't, it also gives incorrect answers!
I changed the line
Number tnum = cf.parse("0.00");
to
Number tnum = cf.parse("0.000000001");
and Number tnum = cf.parse("0.000000000000000002");
and Number tnum = cf.parse("0.000000000000000000000000123");
in an attempt to see if returning 0.0 (or something close) was being used
as an error sentinel (bad idea if true). It looks like any number of
leading zeroes in the fraction part are ignored! That is, it happily says
0.1 for the first, 0.2 for the second and 0.123 for the third. I suspect
someone is reading the integer part and the fraction part separately and
forgetting that leading zeroes are significant once you get past the
decimal separator (a really bad idea if true). I found
src/share/classes/java/text/DecimalFormat.java, which seems to be where the
code is, but it's way complicated to just look through and evaluated the
difficulty of the fix.
... peter
> From sl@taller Thu Apr 24 12:42:19 1997
> Date: Thu, 24 Apr 1997 12:42:31 -0700
> From: sl@taller (Sheng Liang)
> Subject: Any one able to parse 0.00 with NumberFormat?
> To: pbk@taller
>
> Is this a bug, or mearly conformance to the Spec?
>
> Sheng
>
> ------------- Begin Forwarded Message -------------
>
> >From ###@###.### Thu Apr 24 08:00:29 1997
> Delivered-To: ###@###.###
> Delivered-To: ###@###.###
> Date: Thu, 24 Apr 1997 07:31:53 -0600
> From: Joel Riedesel <###@###.###>
> MIME-Version: 1.0
> To: ###@###.###
> Subject: Any one able to parse 0.00 with NumberFormat?
> X-Priority: 3 (Normal)
> Content-Transfer-Encoding: 7bit
>
> Seems that 1.1.1 broke when trying to parse
> "0.00" or "$0.00". Very annoying.
> (It works in JDK 1.1)
>
>
> Here's the test case I put together.
> If I'm doing something wrong that should allow me to parse
> these, please let me know.
> (I've submitted a bug report already.)
>
> import java.text.NumberFormat;
>
> public class CurrTest {
>
> public static void main(String args[]) {
> double testnum = 345.6789;
> long longnum = 23234567;
>
> try {
>
> NumberFormat cf = NumberFormat.getCurrencyInstance();
> System.out.println("cf: " + cf);
> Number tnum = cf.parse("$0.00");
> System.out.println("tnum: " + tnum);
> String result = new Double(tnum.doubleValue()).toString();
> System.out.println("result: " + result);
>
> } catch(Exception e) {
> System.out.println("exception: " + e.getMessage());
> e.printStackTrace();
> }
>
> try {
>
> NumberFormat cf = NumberFormat.getNumberInstance();
> System.out.println("cf: " + cf);
> Number tnum = cf.parse("0.00");
> System.out.println("tnum: " + tnum);
> String result = new Double(tnum.doubleValue()).toString();
> System.out.println("result: " + result);
>
> } catch(Exception e) {
> System.out.println("exception: " + e.getMessage());
> e.printStackTrace();
> }
>
> }
> }
>
>
>
It's worse than that. In addition to throwing exceptions where it
shouldn't, it also gives incorrect answers!
I changed the line
Number tnum = cf.parse("0.00");
to
Number tnum = cf.parse("0.000000001");
and Number tnum = cf.parse("0.000000000000000002");
and Number tnum = cf.parse("0.000000000000000000000000123");
in an attempt to see if returning 0.0 (or something close) was being used
as an error sentinel (bad idea if true). It looks like any number of
leading zeroes in the fraction part are ignored! That is, it happily says
0.1 for the first, 0.2 for the second and 0.123 for the third. I suspect
someone is reading the integer part and the fraction part separately and
forgetting that leading zeroes are significant once you get past the
decimal separator (a really bad idea if true). I found
src/share/classes/java/text/DecimalFormat.java, which seems to be where the
code is, but it's way complicated to just look through and evaluated the
difficulty of the fix.
... peter
> From sl@taller Thu Apr 24 12:42:19 1997
> Date: Thu, 24 Apr 1997 12:42:31 -0700
> From: sl@taller (Sheng Liang)
> Subject: Any one able to parse 0.00 with NumberFormat?
> To: pbk@taller
>
> Is this a bug, or mearly conformance to the Spec?
>
> Sheng
>
> ------------- Begin Forwarded Message -------------
>
> >From ###@###.### Thu Apr 24 08:00:29 1997
> Delivered-To: ###@###.###
> Delivered-To: ###@###.###
> Date: Thu, 24 Apr 1997 07:31:53 -0600
> From: Joel Riedesel <###@###.###>
> MIME-Version: 1.0
> To: ###@###.###
> Subject: Any one able to parse 0.00 with NumberFormat?
> X-Priority: 3 (Normal)
> Content-Transfer-Encoding: 7bit
>
> Seems that 1.1.1 broke when trying to parse
> "0.00" or "$0.00". Very annoying.
> (It works in JDK 1.1)
>
>
> Here's the test case I put together.
> If I'm doing something wrong that should allow me to parse
> these, please let me know.
> (I've submitted a bug report already.)
>
> import java.text.NumberFormat;
>
> public class CurrTest {
>
> public static void main(String args[]) {
> double testnum = 345.6789;
> long longnum = 23234567;
>
> try {
>
> NumberFormat cf = NumberFormat.getCurrencyInstance();
> System.out.println("cf: " + cf);
> Number tnum = cf.parse("$0.00");
> System.out.println("tnum: " + tnum);
> String result = new Double(tnum.doubleValue()).toString();
> System.out.println("result: " + result);
>
> } catch(Exception e) {
> System.out.println("exception: " + e.getMessage());
> e.printStackTrace();
> }
>
> try {
>
> NumberFormat cf = NumberFormat.getNumberInstance();
> System.out.println("cf: " + cf);
> Number tnum = cf.parse("0.00");
> System.out.println("tnum: " + tnum);
> String result = new Double(tnum.doubleValue()).toString();
> System.out.println("result: " + result);
>
> } catch(Exception e) {
> System.out.println("exception: " + e.getMessage());
> e.printStackTrace();
> }
>
> }
> }
>
>
>
- duplicates
-
JDK-4048975 NumberFormat parse throws exceptions on 0
-
- Closed
-