Spinner: added thousand separator format to numbers and refactored code.

This commit is contained in:
Ca-Phun Ung 2008-10-02 18:06:30 +00:00
parent c2b0afd3e7
commit 5958c417f6
2 changed files with 15 additions and 13 deletions

View File

@ -240,7 +240,7 @@ test("spin with auto-incremental stepping", function() {
el.simulate("keyup",{keyCode:$.simulate.VK_DOWN}); el.simulate("keyup",{keyCode:$.simulate.VK_DOWN});
equals(el.val(), -1800, "keydown 210 times (300-100-100*10-10*100)"); equals(el.val(), '-1,800', "keydown 210 times (300-100-100*10-10*100)");
}); });

View File

@ -393,21 +393,23 @@ $.extend($.ui.spinner, {
items: [] items: []
}, },
format: { format: {
number: function(num, dec) {
return this.round(num, dec);
},
currency: function(num, sym) { currency: function(num, sym) {
return (num !== Math.abs(num) ? '-' : '') + sym + this.round(Math.abs(num), 2); num = isNaN(num) ? 0 : num;
return (num !== Math.abs(num) ? '-' : '') + sym + this.number(Math.abs(num), 2);
}, },
round: function(num, dec) { number: function(num, dec) {
var s = Math.round(parseFloat(num)*Math.pow(10, dec)) / Math.pow(10, dec); // round off weird decimals num = isNaN(num) ? 0 : parseFloat(num,10).toFixed(dec);
if (dec > 0) {
s = s + ((s.toString().indexOf('.') == -1) ? '.' : '') + '0000000001'; var regex = /(\d+)(\d{3})/,
s = s.substr(0, s.indexOf('.')+1+dec); n = num.toString().split('.'),
} else { n1 = n[0],
s = Math.round(s); n2 = n.length > 1 ? '.' + n[1] : '';
while (regex.test(n1)) {
n1 = n1.replace(regex, '$1,$2');
} }
return s;
return (n1 + n2);
} }
} }
}); });