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