Add e-notation preprocessing to formulas

SCI-350 #close
This commit is contained in:
Jure Grabnar 2016-09-13 09:41:43 +02:00
parent e7d8c80d29
commit 6519e8dbb1
7 changed files with 19 additions and 2 deletions

View file

@ -65,4 +65,5 @@
<%= javascript_include_tag "parser" %>
<%= javascript_include_tag "ruleJS" %>
<%= javascript_include_tag "handsontable.formula" %>
<%= javascript_include_tag "big.min" %>
<%= stylesheet_link_tag "handsontable.formula" %>

View file

@ -42,4 +42,5 @@
<%= javascript_include_tag "parser" %>
<%= javascript_include_tag "ruleJS" %>
<%= javascript_include_tag "handsontable.formula" %>
<%= javascript_include_tag "big.min" %>
<%= stylesheet_link_tag "handsontable.formula" %>

View file

@ -86,4 +86,5 @@
<%= javascript_include_tag "parser" %>
<%= javascript_include_tag "ruleJS" %>
<%= javascript_include_tag "handsontable.formula" %>
<%= javascript_include_tag "big.min" %>
<%= stylesheet_link_tag "handsontable.formula" %>

View file

@ -71,3 +71,4 @@ Rails.application.config.assets.precompile += %w( parser.js )
Rails.application.config.assets.precompile += %w( ruleJS.js )
Rails.application.config.assets.precompile += %w( handsontable.formula.js )
Rails.application.config.assets.precompile += %w( handsontable.formula.css )
Rails.application.config.assets.precompile += %w( big.min.js )

1
vendor/assets/javascripts/big.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -947,8 +947,8 @@ var Parser = (function(){
break;
}
},
rules: [/^(?:\s+)/,/^(?:"(\\["]|[^"])*")/,/^(?:'(\\[']|[^'])*')/,/^(?:[A-Za-z]{1,}[A-Za-z_0-9]+(?=[(]))/,/^(?:([0]?[1-9]|1[0-2])[:][0-5][0-9]([:][0-5][0-9])?[ ]?(AM|am|aM|Am|PM|pm|pM|Pm))/,/^(?:([0]?[0-9]|1[0-9]|2[0-3])[:][0-5][0-9]([:][0-5][0-9])?)/,/^(?:\$[A-Za-z]+\$[0-9]+)/,/^(?:\$[A-Za-z]+[0-9]+)/,/^(?:[A-Za-z]+\$[0-9]+)/,/^(?:[A-Za-z]+[0-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\b)/,/^(?:")/,/^(?:')/,/^(?:!)/,/^(?:=)/,/^(?:%)/,/^(?:[#])/,/^(?:$)/,/(?:([0-9]+(\.[0-9]+)?)+(E|e)((-|\+)?[0-9]+))/],
conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38, 39],"inclusive":true}}
});
return lexer;
})();

View file

@ -1054,6 +1054,9 @@ var ruleJS = (function (root) {
case '^':
result = Math.pow(number1, number2);
break;
case 'e':
result = number1 * Math.pow(10, number2);
break;
}
return result;
@ -1227,6 +1230,15 @@ var ruleJS = (function (root) {
error = null;
try {
// Preprocess E-notation, eg. replaces -5.32e-3 with -0.00532
// Excel does it too as well, so we're good
formula = formula.replace(/(?:([0-9]+(\.[0-9]+)?)+(E|e)((-|\+)?[0-9]+))/g, function(expr) {
var spl = expr.split('E')
var m = helper.number(spl[0]);
var e = helper.number(spl[1]);
return Big(m).times(Big(10).pow(e)).toFixed();
})
parser.setObj(element);
result = parser.parse(formula);