You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Accountants like to see negative numbers wrapped in parentheses instead of using a minus sign. For negative currency amounts, the currency symbol is placed inside the parens.
Here's my patch that adds paren handling to formatFloat() and updates isDigit(), currency.is(), currency.format(), and percent.is() to allow/preserve them.
formatFloat: The regex of \([.\d]+\) might be a little restrictive, but currency and percent signs have already been stripped out, so these negative numbers shouldn't be any more complex than "(1234.56)". Replacing ( with - seemed simpler than adding a flag to flip the sign after parseFloat().
isDigit: I added the parens to the test regex so they're in the same group as +/-, instead of stripping them out in the replace.
currency/percent is(): Allow for optional parens before currency symbol or after percent sign.
currency.format(): Preserve parens so they get passed to formatFloat.
I think this covers all the numberic parsers without adding much complexity. I didn't make this a flag like usNumberFormat because that would overcomplicate the regexes, and these changes should handle both minus-sign and parentheses negatives interchangeably.
--- jquery.tablesorter.js 2012-04-16 14:58:22.615183200 -0400
+++ jquery.tablesorter.negativeparens.js 2012-04-16 15:02:12.971317400 -0400
@@ -768,13 +768,16 @@
// French Format = 1 234 567,89 -> 1234567.89
s = s.replace(/[\s|\.]/g,'').replace(/,/g,'.');
}
+ if(/^\s*\([.\d]+\)/.test(s)) {
+ s = s.replace(/^\s*\(/,'-').replace(/\)/,'');
+ }
var i = parseFloat(s);
// return the text instead of zero
return isNaN(i) ? $.trim(s) : i;
};
this.isDigit = function(s) {
// replace all unwanted chars and match.
- return (/^[\-+]?\d*$/).test($.trim(s.replace(/[,.'\s]/g, '')));
+ return (/^[\-+(]?\d*[)]?$/).test($.trim(s.replace(/[,.'\s]/g, '')));
};
this.clearTableBody = function (table) {
$(table.tBodies[0]).empty();
@@ -816,10 +819,10 @@
ts.addParser({
id: "currency",
is: function(s){
- return (/^[\u00a3$\u20ac\u00a4\u00a5\u00a2?.]/).test(s); // £$€¤¥¢?.
+ return (/^\(?[\u00a3$\u20ac\u00a4\u00a5\u00a2?.]/).test(s); // £$€¤¥¢?.
},
format: function(s){
- return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9,. \-]/g), ""));
+ return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9,. \-()]/g), ""));
},
type: "numeric"
});
@@ -871,7 +874,7 @@
ts.addParser({
id: "percent",
is: function(s) {
- return (/\%$/).test($.trim(s));
+ return (/\%\)?$/).test($.trim(s));
},
format: function(s) {
return $.tablesorter.formatFloat(s.replace(new RegExp(/%/g), ""));
The text was updated successfully, but these errors were encountered:
Accountants like to see negative numbers wrapped in parentheses instead of using a minus sign. For negative currency amounts, the currency symbol is placed inside the parens.
Here's my patch that adds paren handling to
formatFloat()
and updatesisDigit()
,currency.is()
,currency.format()
, andpercent.is()
to allow/preserve them.formatFloat: The regex of
\([.\d]+\)
might be a little restrictive, but currency and percent signs have already been stripped out, so these negative numbers shouldn't be any more complex than "(1234.56)". Replacing(
with-
seemed simpler than adding a flag to flip the sign after parseFloat().isDigit: I added the parens to the test regex so they're in the same group as +/-, instead of stripping them out in the replace.
currency/percent is(): Allow for optional parens before currency symbol or after percent sign.
currency.format(): Preserve parens so they get passed to formatFloat.
I think this covers all the numberic parsers without adding much complexity. I didn't make this a flag like usNumberFormat because that would overcomplicate the regexes, and these changes should handle both minus-sign and parentheses negatives interchangeably.
The text was updated successfully, but these errors were encountered: