You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by ra...@apache.org on 2005/10/25 19:01:48 UTC

svn commit: r328418 - /xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java

Author: radup
Date: Tue Oct 25 10:01:43 2005
New Revision: 328418

URL: http://svn.apache.org/viewcvs?rev=328418&view=rev
Log:
Fix for XMLBEANS-208. Wasn't calculating the fractionDigits correctly. Fixed totalDigits while I was there too.

Contributed by Lawrence Jones

Modified:
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java?rev=328418&r1=328417&r2=328418&view=diff
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/JavaDecimalHolderEx.java Tue Oct 25 10:01:43 2005
@@ -88,10 +88,19 @@
         if (fd != null)
         {
             int scale = ((XmlObjectBase)fd).bigIntegerValue().intValue();
-            if (v.scale() > scale)
+            try
             {
+                // used only for side-effect - this does not change v despite
+                // the name of the method
+                v.setScale(scale);
+            }
+            catch(ArithmeticException e)
+            {
+                // ArithmeticException will be thrown if cannot represent as an Integer
+                // with this scale - i.e. would need a fraction which would correspond
+                // to digits beyond the allowed number
                 context.invalid(XmlErrorCodes.DATATYPE_FRACTION_DIGITS_VALID,
-                    new Object[] { new Integer(v.scale()), v, new Integer(scale), QNameHelper.readable(sType) });
+                    new Object[] { new Integer(v.scale()), v.toString(), new Integer(scale), QNameHelper.readable(sType) });
                 return;
             }
         }
@@ -102,13 +111,33 @@
         {
             String temp = v.unscaledValue().toString();
             int tdf = ((XmlObjectBase)td).bigIntegerValue().intValue();
-            int len = temp.length();
-            if (len > 0 && temp.charAt(0) == '-')
-                len -= 1;
+            int origLen = temp.length();
+            int len = origLen;
+            if (origLen > 0)
+            {
+                // don't count leading minus
+                if (temp.charAt(0) == '-')
+                {
+                    len -= 1;
+                }
+
+                // don't count trailing zeros if we can absorb them into scale
+                int insignificantTrailingZeros = 0;
+                int vScale = v.scale();
+                for(int j = origLen-1;
+                    temp.charAt(j) == '0' && j > 0 && insignificantTrailingZeros < vScale;
+                    j--)
+                {
+                    insignificantTrailingZeros++;
+                }
+
+                len -= insignificantTrailingZeros;
+            }
+
             if (len > tdf)
             {
                 context.invalid(XmlErrorCodes.DATATYPE_TOTAL_DIGITS_VALID,
-                    new Object[] { new Integer(len), temp, new Integer(tdf), QNameHelper.readable(sType) });
+                    new Object[] { new Integer(len), v.toString(), new Integer(tdf), QNameHelper.readable(sType) });
                 return;
             }
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org