Allow using formatFloat without providing a table

This commit is contained in:
Mottie 2012-12-19 09:06:19 -06:00
parent bb4987bba9
commit 8749473da1
2 changed files with 10 additions and 3 deletions

View File

@ -1129,7 +1129,11 @@
ts.formatFloat = function(s, table) {
if (typeof(s) !== 'string' || s === '') { return s; }
if (table.config.usNumberFormat !== false) {
// allow using formatFloat without a table; defaults to US number format
var i,
t = table && table.hasOwnProperty('config') ? table.config.usNumberFormat !== false :
typeof table !== "undefined" ? table : true;
if (t) {
// US Format - 1,234,567.89 -> 1234567.89
s = s.replace(/,/g,'');
} else {
@ -1141,7 +1145,7 @@
// make (#) into a negative number -> (10) = -10
s = s.replace(/^\s*\(/,'-').replace(/\)/,'');
}
var i = parseFloat(s);
i = parseFloat(s);
// return the text instead of zero
return isNaN(i) ? $.trim(s) : i;
};

View File

@ -93,11 +93,13 @@
return ts.formatFloat(str, table1);
};
test( "formatFloat", function() {
expect(17);
expect(18);
strictEqual( ff(''), '', 'returns empty string' );
strictEqual( ff(5), 5, 'returns numerical values');
c1.usNumberFormat = false;
strictEqual( ts.formatFloat('1,234,567.89'), 1234567.89, 'use format float without including table - defaults to US number format');
strictEqual( ff('1 234,56'), 1234.56, 'parse non-U.S. (French) number format');
strictEqual( ff('1.234,56'), 1234.56, 'parse non-U.S. (German) number format');
strictEqual( ff('-32,32'), -32.32, 'negative non-U.S. signed numbers');
@ -116,6 +118,7 @@
strictEqual( ff('fred 12'), 'fred 12', 'return string if number not at beginning');
strictEqual( ff('12fred'), 12, 'parse number + string into number only');
strictEqual( ff('(fred)'), '(fred)', 'leave parenthesis intact on strings');
});
/************************************************