Update of /var/cvs/src/org/mmbase/datatypes
In directory james.mmbase.org:/tmp/cvs-serv19855
Modified Files:
DecimalDataType.java NumberDataType.java
Log Message:
Locale specific parsing didn't work for decimals
See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/datatypes
Index: DecimalDataType.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/datatypes/DecimalDataType.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- DecimalDataType.java 26 Jan 2009 16:28:31 -0000 1.5
+++ DecimalDataType.java 4 Mar 2009 17:45:21 -0000 1.6
@@ -36,7 +36,7 @@
*
*
* @author Michiel Meeuwissen
- * @version $Id: DecimalDataType.java,v 1.5 2009/01/26 16:28:31 michiel Exp $
+ * @version $Id: DecimalDataType.java,v 1.6 2009/03/04 17:45:21 michiel Exp $
* @since MMBase-1.9.1
*/
public class DecimalDataType extends NumberDataType<BigDecimal> implements
LengthDataType<BigDecimal> {
@@ -95,17 +95,11 @@
@Override protected BigDecimal castString(Object preCast, Cloud cloud)
throws CastException {
if (preCast == null || "".equals(preCast)) return null;
- if (preCast instanceof CharSequence) {
- try {
- BigDecimal dec = new BigDecimal("" + preCast, new
MathContext(Integer.MAX_VALUE, roundingMode));
- return dec;
- } catch (NumberFormatException nfe) {
- throw new CastException(nfe);
- } catch (ArithmeticException ae) {
- throw new CastException(ae);
- }
+ Number su = super.castString(preCast, cloud);
+ if (su instanceof BigDecimal) {
+ return ((BigDecimal) su).round(new MathContext(Integer.MAX_VALUE,
roundingMode));
} else {
- return Casting.toDecimal(preCast);
+ throw new CastException("Not a big decimal " + preCast);
}
}
Index: NumberDataType.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/datatypes/NumberDataType.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -b -r1.36 -r1.37
--- NumberDataType.java 4 Mar 2009 11:32:09 -0000 1.36
+++ NumberDataType.java 4 Mar 2009 17:45:21 -0000 1.37
@@ -22,7 +22,7 @@
* A DataType representing some kind of numeric value, like a floating point
number or an integer number.
*
* @author Pierre van Rooden
- * @version $Id: NumberDataType.java,v 1.36 2009/03/04 11:32:09 michiel Exp $
+ * @version $Id: NumberDataType.java,v 1.37 2009/03/04 17:45:21 michiel Exp $
* @since MMBase-1.8
*/
abstract public class NumberDataType<E extends Number & Comparable<E>> extends
ComparableDataType<E> {
@@ -39,17 +39,19 @@
protected Number castString(Object preCast, Cloud cloud) throws
CastException {
if (preCast == null || "".equals(preCast)) return null;
- if (preCast instanceof String) {
-
- String s = (String) preCast;
- try {
- return Casting.toDecimal(s);
- } catch (NumberFormatException nfe) {
- log.debug(nfe);
+ if (preCast instanceof CharSequence) {
+ String s = preCast.toString();
Locale l = cloud != null ? cloud.getLocale() :
Locale.getDefault();
NumberFormat nf = NumberFormat.getNumberInstance(l);
- nf.setGroupingUsed(false); // we never want to parse e.g.
"1.2" to "12". It simply makes
- // no sense, and hard to make backwards compatible
+
+ try {
+ nf.setGroupingUsed(false); // we never want to parse e.g.
"1.2" to "12". It simply
+ // makes no sense, and hard to make
backwards compatible
+ if (nf instanceof DecimalFormat) {
+ ((DecimalFormat) nf).setParseBigDecimal(true);
+ } else {
+ log.warn("Not a DecimalFormat");
+ }
ParsePosition p = new ParsePosition(0);
Number number = nf.parse(s, p);
if (log.isDebugEnabled()) {
@@ -65,14 +67,19 @@
return Casting.toDecimal(s);
}
}
- return Casting.toDecimal(number);
+ return number;
+ } catch (NumberFormatException nfe) {
+ log.debug("For " + nf + " " + nfe.getMessage());
}
+ return Casting.toDecimal(s);
} else if (preCast instanceof Float) {
if (((Float) preCast).isInfinite()) {
+ // not supported by decimal
return (Float) preCast;
}
} else if (preCast instanceof Double) {
if (((Double) preCast).isInfinite()) {
+ // not supported by decimal
return (Double) preCast;
}
}
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs