This repository has been archived by the owner on Jul 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
jquery.formatNumber-0.1.1.js
71 lines (68 loc) · 2.13 KB
/
jquery.formatNumber-0.1.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* jQuery formatNumber v0.1.1
* https://github.com/RaphaelDDL/jquery.formatNumber
*
* Copyright (c) 2012 Raphael "DDL" Oliveira
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License (CC BY-NC-SA 3.0)
* http://creativecommons.org/licenses/by-nc-sa/3.0/
*
*
* ===============
* Based on:
* 'addCommas' function http://www.mredkj.com/javascript/numberFormat.html
* Copyright (c) 2011 novusoft LLC
*
*
* ===============
* Special thanks to:
* - Queness and it's jQuery Plugin Tutorial ( http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial )
* - All people who helped jQuery be what is.
*
* ===============
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* ===============
*/
(function($){
/* ===============
// $(Elem).formatNumber({opts});
// =============== */
$.fn.extend({
formatNumber: function(options){
var defaults = {
cents: '.',
decimal: ','
}
var o = $.extend(defaults, options);
return this.each(function() {
/* ----Script Start---- */
var thiz = $(this), values, x, x1, x2;
//try{
values = $.trim(thiz.html());
//console.log(values);
values += '';
x = values.split(o.cents);
//console.log(x);
x1 = x[0];
//console.log(x1);
x2 = x.length > 1 ? o.cents + x[1] : '';
//console.log(x2);
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + o.decimal + '$2');
}
thiz.html(x1 + x2);
//}catch(e){
// thiz.html('Value ('+values+') not formatable.');
//}
/* ----Script End---- */
});//return each
}//fn.extend
});
})(jQuery);