aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCa-Phun Ung <pazu2k@gmail.com>2008-10-02 18:06:30 +0000
committerCa-Phun Ung <pazu2k@gmail.com>2008-10-02 18:06:30 +0000
commit5958c417f65cf779845cae2d107926fbaf6c7f7f (patch)
treeed8e6a5d1e5eb0338419ff4ba45f7843ce0d744f
parentc2b0afd3e7dce76f01fa6661993cbf08105d22cb (diff)
downloadjquery-ui-5958c417f65cf779845cae2d107926fbaf6c7f7f.tar.gz
jquery-ui-5958c417f65cf779845cae2d107926fbaf6c7f7f.zip
Spinner: added thousand separator format to numbers and refactored code.
-rw-r--r--tests/spinner.js2
-rw-r--r--ui/ui.spinner.js26
2 files changed, 15 insertions, 13 deletions
diff --git a/tests/spinner.js b/tests/spinner.js
index 507d2c897..ef20274ab 100644
--- a/tests/spinner.js
+++ b/tests/spinner.js
@@ -240,7 +240,7 @@ test("spin with auto-incremental stepping", function() {
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)");
});
diff --git a/ui/ui.spinner.js b/ui/ui.spinner.js
index 8fc960dc3..9f6567a76 100644
--- a/ui/ui.spinner.js
+++ b/ui/ui.spinner.js
@@ -393,21 +393,23 @@ $.extend($.ui.spinner, {
items: []
},
format: {
- number: function(num, dec) {
- return this.round(num, dec);
- },
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) {
- var s = Math.round(parseFloat(num)*Math.pow(10, dec)) / Math.pow(10, dec); // round off weird decimals
- if (dec > 0) {
- s = s + ((s.toString().indexOf('.') == -1) ? '.' : '') + '0000000001';
- s = s.substr(0, s.indexOf('.')+1+dec);
- } else {
- s = Math.round(s);
+ number: function(num, dec) {
+ num = isNaN(num) ? 0 : parseFloat(num,10).toFixed(dec);
+
+ var regex = /(\d+)(\d{3})/,
+ n = num.toString().split('.'),
+ n1 = n[0],
+ n2 = n.length > 1 ? '.' + n[1] : '';
+
+ while (regex.test(n1)) {
+ n1 = n1.replace(regex, '$1,$2');
}
- return s;
+
+ return (n1 + n2);
}
}
});