-
Notifications
You must be signed in to change notification settings - Fork 52
/
jquery.currency.js
219 lines (197 loc) · 9.85 KB
/
jquery.currency.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* jQuery Currency v0.6 ( January 2015 )
* Simple, unobtrusive currency converting and formatting
*
* Copyright 2015, sMarty
* Free to use and abuse under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* http://coderspress.com
*
*/
(function ($) {
$.fn.currency = function (method) {
var methods = {
init: function (options) {
var settings = $.extend({}, this.currency.defaults, options);
return this.each(function () {
var $element = $(this),
element = this;
var value = 0;
if ($element.is(':input')) {
value = $element.val();
} else {
value = $element.text();
}
if (helpers.isNumber(value)) {
if (settings.convertFrom != '') {
if ($element.is(':input')) {
$element.val(value + ' ' + settings.convertLoading);
} else {
$element.html(value + ' ' + settings.convertLoading);
}
$.post(settings.convertLocation, {
amount: value,
from: settings.convertFrom,
to: settings.region
}, function (data) {
value = data;
if ($element.is(':input')) {
$element.val(helpers.format_currency(value, settings));
} else {
$element.html(helpers.format_currency(value, settings));
}
});
} else {
if ($element.is(':input')) {
$element.val(helpers.format_currency(value, settings));
} else {
$element.html(helpers.format_currency(value, settings));
}
}
}
});
},
}
var helpers = {
format_currency: function (amount, settings) {
var bc = settings.region;
var currency_before = '';
var currency_after = '';
if (bc == 'ALL') currency_before = 'Lek';
if (bc == 'ARS') currency_before = '$';
if (bc == 'AWG') currency_before = 'f';
if (bc == 'AUD') currency_before = '$';
if (bc == 'BSD') currency_before = '$';
if (bc == 'BBD') currency_before = '$';
if (bc == 'BYR') currency_before = 'p.';
if (bc == 'BZD') currency_before = 'BZ$';
if (bc == 'BMD') currency_before = '$';
if (bc == 'BOB') currency_before = '$b';
if (bc == 'BAM') currency_before = 'KM';
if (bc == 'BWP') currency_before = 'P';
if (bc == 'BRL') currency_before = 'R$';
if (bc == 'BND') currency_before = '$';
if (bc == 'CAD') currency_before = '$';
if (bc == 'KYD') currency_before = '$';
if (bc == 'CLP') currency_before = '$';
if (bc == 'CNY') currency_before = '¥';
if (bc == 'COP') currency_before = '$';
if (bc == 'CRC') currency_before = 'c';
if (bc == 'HRK') currency_before = 'kn';
if (bc == 'CZK') currency_before = 'Kc';
if (bc == 'DKK') currency_before = 'kr';
if (bc == 'DOP') currency_before = 'RD$';
if (bc == 'XCD') currency_before = '$';
if (bc == 'EGP') currency_before = '£';
if (bc == 'SVC') currency_before = '$';
if (bc == 'EEK') currency_before = 'kr';
if (bc == 'EUR') currency_before = '€';
if (bc == 'FKP') currency_before = '£';
if (bc == 'FJD') currency_before = '$';
if (bc == 'GBP') currency_before = '£';
if (bc == 'GHC') currency_before = 'c';
if (bc == 'GIP') currency_before = '£';
if (bc == 'GTQ') currency_before = 'Q';
if (bc == 'GGP') currency_before = '£';
if (bc == 'GYD') currency_before = '$';
if (bc == 'HNL') currency_before = 'L';
if (bc == 'HKD') currency_before = '$';
if (bc == 'HUF') currency_before = 'Ft';
if (bc == 'ISK') currency_before = 'kr';
if (bc == 'IDR') currency_before = 'Rp';
if (bc == 'IMP') currency_before = '£';
if (bc == 'JMD') currency_before = 'J$';
if (bc == 'JPY') currency_before = '¥';
if (bc == 'JEP') currency_before = '£';
if (bc == 'LVL') currency_before = 'Ls';
if (bc == 'LBP') currency_before = '£';
if (bc == 'LRD') currency_before = '$';
if (bc == 'LTL') currency_before = 'Lt';
if (bc == 'MYR') currency_before = 'RM';
if (bc == 'MXN') currency_before = '$';
if (bc == 'MZN') currency_before = 'MT';
if (bc == 'NAD') currency_before = '$';
if (bc == 'ANG') currency_before = 'f';
if (bc == 'NZD') currency_before = '$';
if (bc == 'NIO') currency_before = 'C$';
if (bc == 'NOK') currency_before = 'kr';
if (bc == 'PAB') currency_before = 'B/.';
if (bc == 'PYG') currency_before = 'Gs';
if (bc == 'PEN') currency_before = 'S/.';
if (bc == 'PLN') currency_before = 'zl';
if (bc == 'RON') currency_before = 'lei';
if (bc == 'SHP') currency_before = '£';
if (bc == 'SGD') currency_before = '$';
if (bc == 'SBD') currency_before = '$';
if (bc == 'SOS') currency_before = 'S';
if (bc == 'ZAR') currency_before = 'R';
if (bc == 'SEK') currency_before = 'kr';
if (bc == 'CHF') currency_before = 'CHF';
if (bc == 'SRD') currency_before = '$';
if (bc == 'SYP') currency_before = '£';
if (bc == 'TWD') currency_before = 'NT$';
if (bc == 'TTD') currency_before = 'TT$';
if (bc == 'TRY') currency_before = 'TL';
if (bc == 'TRL') currency_before = '£';
if (bc == 'TVD') currency_before = '$';
if (bc == 'GBP') currency_before = '£';
if (bc == 'USD') currency_before = '$';
if (bc == 'UYU') currency_before = '$U';
if (bc == 'VEF') currency_before = 'Bs';
if (bc == 'ZWD') currency_before = 'Z$';
if (currency_before == '' && currency_after == '') currency_before = '$';
var output = '';
if (!settings.hidePrefix) output += currency_before;
output += helpers.number_format(amount, settings.decimals, settings.decimal, settings.thousands);
if (!settings.hidePostfix) output += currency_after;
return output;
},
// Kindly borrowed from http://phpjs.org/functions/number_format
number_format: function (number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
},
isNumber: function (n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
}
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method "' + method + '" does not exist in currency plugin!');
}
}
$.fn.currency.defaults = {
region: "USD", // The 3 digit ISO code you want to display your currency in
thousands: ",", // Thousands separator
decimal: ".", // Decimal separator
decimals: 2, // How many decimals to show
hidePrefix: false, // Hide any prefix
hidePostfix: false, // Hide any postfix
convertFrom: "", // If converting, the 3 digit ISO code you want to convert from,
convertLoading: "(Converting...)", // Loading message appended to values while converting
convertLocation: "convert.php" // Location of convert.php file
}
$.fn.currency.settings = {}
})(jQuery);