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:31 UTC

[myfaces-trinidad] 08/36: TRINIDAD-1678 TrNumberFormat.prototype.numberToString doesn't account for numbers represented via scientific notation commit for Cale Scholl

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

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

commit c96ad4c92b32bdabb6ac0dbd4ffdfc8353f2301c
Author: Jeanne Waldman <jw...@apache.org>
AuthorDate: Thu Jan 21 04:05:15 2010 +0000

    TRINIDAD-1678 TrNumberFormat.prototype.numberToString doesn't account for numbers represented via scientific notation
    commit for Cale Scholl
---
 .../javascript/META-INF/adf/jsLibs/CoreFormat.js   |  8 ++-
 .../javascript/META-INF/adf/jsLibs/NumberFormat.js | 68 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js b/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js
index 0304a3a..8dbbd55 100644
--- a/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js
+++ b/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js
@@ -957,15 +957,17 @@ function _decimalParse(
     var floater = false;
     if (parsefloat != null)
     {
-      // Why parseInt(parseFloat(numberString))? Because the server NumberConverter behaves the same 
-      // way as parseFloat. Note the following:
+      // Why trim leading zeroes? parseFloat behaves the same way as the server NumberConverter, 
+      // but parseInt interprets octal, and thus we need to trim leading zeroes. 
+      // Note the following:
       // parseInt interprets octal and hex:
       //   alert(parseInt("0xA")); // returns 10
       //   alert(parseInt("008")); // returns 0, as it stops parsing octal at the first invalid character, 8
       // parseFloat interprets neither octal nor hex:
       //   alert(parseFloat("0xA")); // returns 0, as it stops parsing decimal at the first invalid character, x
       //   alert(parseFloat("008")); // returns 8
-      result = parsefloat ? parseFloat(numberString) : parseInt(parseFloat(numberString));
+      numberString = TrNumberFormat.trimLeadingZeroes(numberString);
+      result = parsefloat ? parseFloat(numberString) : parseInt(numberString);
     }
     else
     {
diff --git a/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js b/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
index 5a030f4..0f2ca76 100644
--- a/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
+++ b/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
@@ -338,6 +338,10 @@ TrNumberFormat.prototype.numberToString = function(number)
     number = (number*-1);
 
   var numberString = number + "";
+  
+  // check for scientific notation
+  numberString = TrNumberFormat.scientificToExpanded(numberString);
+  
   var index = numberString.indexOf(".");
   var numberStringLength = numberString.length;
   var ints;
@@ -418,6 +422,70 @@ TrNumberFormat.prototype.percentageToString = function(number)
 }
 
 /**
+ * Static utility function.
+ * Converts a number string from scientific notation to standard expanded notation.
+ */
+TrNumberFormat.scientificToExpanded = function(numberString)
+{
+  // check for scientific notation
+  var expIndex = numberString.indexOf('e');
+  if (expIndex == -1)
+    return numberString;
+    
+  var prefix = "";
+  if (numberString.charAt(0) == '-')
+  {
+    prefix = "-";
+    numberString = numberString.substring(1);
+    expIndex -= 1;
+  }
+  
+  var isPosExp = numberString.charAt(expIndex + 1) == '+';
+  var exp = parseInt(numberString.substring(expIndex + 2));
+  var nFractionDigits = expIndex - 2;
+  var zeroes = "";
+  
+  // The exponent should always be greater than the number of fraction digits.
+  if (isPosExp)
+  {
+    for (var i = 0; i < exp - nFractionDigits; ++i)
+      zeroes += "0";
+      
+    return prefix + numberString.charAt(0) + numberString.substring(2, expIndex) + zeroes;
+  }
+  
+  // ELSE: negative exponent
+  for (var i = 0; i < exp - 1; ++i)
+    zeroes += "0";
+    
+  return prefix + "0." + zeroes + numberString.charAt(0) + numberString.substring(2, expIndex);
+}
+
+/**
+ * Static utility function.
+ * Trims extraneous leading zeroes.
+ */
+TrNumberFormat.trimLeadingZeroes = function(numberString)
+{
+  var strbuf = [];
+  var i, char;
+  for (i = 0; i < numberString.length; ++i)
+  {
+    char = numberString.charAt(i);
+    
+    if ((char >= '1' && char <= '9') || char == '.')
+      break;
+      
+    if (char == '0' && i+1 < numberString.length && numberString.charAt(i+1) != '.')
+      continue;
+      
+    strbuf.push(char);
+  }
+  
+  return strbuf.join('') + numberString.substring(i);
+}
+
+/**
  * helper for rounding values
  */
 TrNumberFormat.prototype.getRounded = function(val)

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