Fix issue #2205. Let the parse method throw a NumberFormatException if text cannot be converted into a number, instead of returning 0.0.
Patch by: shanjian
Review by: rdayal
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2211 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/i18n/client/NumberFormat.java b/user/src/com/google/gwt/i18n/client/NumberFormat.java
index 8c67678..a01a477 100644
--- a/user/src/com/google/gwt/i18n/client/NumberFormat.java
+++ b/user/src/com/google/gwt/i18n/client/NumberFormat.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2007 Google Inc.
+ * Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -543,7 +543,7 @@
* @throws NumberFormatException if the entire text could not be converted
* into a number
*/
- public double parse(String text) {
+ public double parse(String text) throws NumberFormatException {
int[] pos = {0};
double result = parse(text, pos);
if (pos[0] == 0 || pos[0] != text.length()) {
@@ -569,8 +569,9 @@
* @param inOutPos position to pass in and get back
* @return a double value representing the parsed number, or <code>0.0</code>
* if the parse fails
+ * @throws NumberFormatException if the text segment could not be converted into a number
*/
- public double parse(String text, int[] inOutPos) {
+ public double parse(String text, int[] inOutPos) throws NumberFormatException {
int start = inOutPos[0];
boolean gotPositive, gotNegative;
double ret = 0.0;
@@ -805,14 +806,11 @@
break;
}
}
-
- try {
- ret = Double.parseDouble(normalizedText.toString());
- ret = ret / scale;
- return ret;
- } catch (NumberFormatException e) {
- return 0.0;
- }
+
+ // parseDouble could throw NumberFormatException, let it do it.
+ ret = Double.parseDouble(normalizedText.toString());
+ ret = ret / scale;
+ return ret;
}
/**
diff --git a/user/test/com/google/gwt/i18n/client/NumberParse_en_Test.java b/user/test/com/google/gwt/i18n/client/NumberParse_en_Test.java
index 427cba2..047dded 100644
--- a/user/test/com/google/gwt/i18n/client/NumberParse_en_Test.java
+++ b/user/test/com/google/gwt/i18n/client/NumberParse_en_Test.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006 Google Inc.
+ * Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -46,6 +46,12 @@
value = numberParse("0.0000", "-123.4579");
assertTrue(value.doubleValue() == -123.4579);
+
+ try {
+ NumberFormat.getDecimalFormat().parse("-1-1--1");
+ fail("Expecting NumberFormatException to be thrown");
+ } catch (NumberFormatException e) {
+ }
}
public void testExponentParse() {