You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2017/09/17 12:30:21 UTC

[myfaces-trinidad] 01/01: TRINIDAD-1683 client-side convertNumber causes loss of precision

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1.2.12.1-branch
in repository https://gitbox.apache.org/repos/asf/myfaces-trinidad.git

commit 25327940224f426afccc167666f29cb23ff9cf00
Author: Gabrielle Crawford <gc...@apache.org>
AuthorDate: Tue Jan 3 22:03:18 2012 +0000

    TRINIDAD-1683 client-side convertNumber causes loss of precision
---
 .../META-INF/adf/jsLibs/NumberConverter.js         | 41 +++++++++++++++++++---
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js b/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
index 96f266c..2f583b0 100644
--- a/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
+++ b/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
@@ -219,7 +219,9 @@ TrNumberConverter.prototype.getAsObject = function(
   label
   )
 {
-  if(this._isConvertible())
+  numberString = TrUIUtils.trim(numberString);
+  
+  if(this._isConvertible(numberString))
   {
     // The following are from the javadoc for Number and DateTimeConverter.
     // If the specified String is null, return a null. Otherwise, trim leading and trailing whitespace before proceeding.
@@ -227,7 +229,6 @@ TrNumberConverter.prototype.getAsObject = function(
     if (numberString == null)
       return null;
     
-    numberString = TrUIUtils.trim(numberString);
     if (numberString.length == 0)
       return null
 
@@ -325,10 +326,42 @@ TrNumberConverter.prototype.getAsObject = function(
  * Checks if this converter can convert the value, which
  * is only true, if no pattern is set and the type is a number
  */
-TrNumberConverter.prototype._isConvertible = function()
+TrNumberConverter.prototype._isConvertible = function(numberString)
 {
   // The locale attribute is now supported on convertNumber.
-  return (this._pattern == null);
+  if (this._pattern != null)
+    return false;
+    
+  // javascript numbers are really doubles, and as such can accurately support 15 digits, see
+  //    http://en.wikipedia.org/wiki/Double_precision
+  //
+  // this means in certain cases a long value that will be fine on the server will be
+  // rounded by the client converter. To avoid this parse the number string, and don't 
+  // try to convert on the client if the number of digits is greater than 15. 
+  // 
+  // Of course this is an imperfect fix, but since the vast majority of 
+  // numbers entered are less than 15 digits numbers are still converted on the client most 
+  // of the time.
+  if (numberString != null)
+  {
+    var nums = 0;
+    
+    for (var i = 0; i < numberString.length; i++)
+    {
+      var charCode = numberString.charCodeAt(i);
+      // the charcode for "0" is 48, the charcode for "9" is 57, so count anything between these 
+      // as a number
+      if (charCode > 47 && charCode < 58)
+      {
+        nums++;
+      }
+    }
+    
+    if (nums > 15)
+      return false;    
+  }
+    
+  return true;
 }
 
 /**

-- 
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.