Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for negative numbers in parentheses #47

Closed
timkingman opened this issue Apr 16, 2012 · 1 comment
Closed

Allow for negative numbers in parentheses #47

timkingman opened this issue Apr 16, 2012 · 1 comment

Comments

@timkingman
Copy link

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), ""));

@Mottie
Copy link
Owner

Mottie commented Apr 16, 2012

Thanks! I've updated tablesorter to 2.1.12 with your enhancements!

I've also updated the dealing with digits demo.

@Mottie Mottie closed this as completed Apr 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants