| 7272 |
amit.gupta |
1 |
/*
|
|
|
2 |
* @Copyright (c) 2010 Ricardo Andrietta Mendes - eng.rmendes@gmail.com
|
|
|
3 |
*
|
|
|
4 |
* Permission is hereby granted, free of charge, to any person
|
|
|
5 |
* obtaining a copy of this software and associated documentation
|
|
|
6 |
* files (the "Software"), to deal in the Software without
|
|
|
7 |
* restriction, including without limitation the rights to use,
|
|
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
9 |
* copies of the Software, and to permit persons to whom the
|
|
|
10 |
* Software is furnished to do so, subject to the following
|
|
|
11 |
* conditions:
|
|
|
12 |
* The above copyright notice and this permission notice shall be
|
|
|
13 |
* included in all copies or substantial portions of the Software.
|
|
|
14 |
*
|
|
|
15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
16 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
17 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
18 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
19 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
20 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
22 |
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
23 |
*
|
|
|
24 |
* How to use it:
|
|
|
25 |
* var formated_value = $().number_format(final_value);
|
|
|
26 |
*
|
|
|
27 |
* Advanced:
|
|
|
28 |
* var formated_value = $().number_format(final_value,
|
|
|
29 |
* {
|
|
|
30 |
* numberOfDecimals:3,
|
|
|
31 |
* decimalSeparator: '.',
|
|
|
32 |
* thousandSeparator: ',',
|
|
|
33 |
* symbol: 'R$'
|
|
|
34 |
* });
|
|
|
35 |
*/
|
|
|
36 |
//indica que está sendo criado um plugin
|
|
|
37 |
jQuery.fn.extend({ //indica que está sendo criado um plugin
|
|
|
38 |
|
|
|
39 |
number_format: function(numero, params) //indica o nome do plugin que será criado com os parametros a serem informados
|
|
|
40 |
{
|
|
|
41 |
//parametros default
|
|
|
42 |
var sDefaults =
|
|
|
43 |
{
|
|
|
44 |
numberOfDecimals: 2,
|
|
|
45 |
decimalSeparator: ',',
|
|
|
46 |
thousandSeparator: '.',
|
|
|
47 |
symbol: ''
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
//função do jquery que substitui os parametros que não foram informados pelos defaults
|
|
|
51 |
var options = jQuery.extend(sDefaults, params);
|
|
|
52 |
|
|
|
53 |
//CORPO DO PLUGIN
|
|
|
54 |
var number = numero;
|
|
|
55 |
var decimals = options.numberOfDecimals;
|
|
|
56 |
var dec_point = options.decimalSeparator;
|
|
|
57 |
var thousands_sep = options.thousandSeparator;
|
|
|
58 |
var currencySymbol = options.symbol;
|
|
|
59 |
|
|
|
60 |
var exponent = "";
|
|
|
61 |
var numberstr = number.toString ();
|
|
|
62 |
var eindex = numberstr.indexOf ("e");
|
|
|
63 |
if (eindex > -1)
|
|
|
64 |
{
|
|
|
65 |
exponent = numberstr.substring (eindex);
|
|
|
66 |
number = parseFloat (numberstr.substring (0, eindex));
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
if (decimals != null)
|
|
|
70 |
{
|
|
|
71 |
var temp = Math.pow (10, decimals);
|
|
|
72 |
number = Math.round (number * temp) / temp;
|
|
|
73 |
}
|
|
|
74 |
var sign = number < 0 ? "-" : "";
|
|
|
75 |
var integer = (number > 0 ?
|
|
|
76 |
Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
|
|
|
77 |
|
|
|
78 |
var fractional = number.toString ().substring (integer.length + sign.length);
|
|
|
79 |
dec_point = dec_point != null ? dec_point : ".";
|
|
|
80 |
fractional = decimals != null && decimals > 0 || fractional.length > 1 ?
|
|
|
81 |
(dec_point + fractional.substring (1)) : "";
|
|
|
82 |
if (decimals != null && decimals > 0)
|
|
|
83 |
{
|
|
|
84 |
for (i = fractional.length - 1, z = decimals; i < z; ++i)
|
|
|
85 |
fractional += "0";
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ?
|
|
|
89 |
thousands_sep : null;
|
|
|
90 |
if (thousands_sep != null && thousands_sep != "")
|
|
|
91 |
{
|
|
|
92 |
for (i = integer.length - 3; i > 0; i -= 3)
|
|
|
93 |
integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if (options.symbol == '')
|
|
|
97 |
{
|
|
|
98 |
return sign + integer + fractional + exponent;
|
|
|
99 |
}
|
|
|
100 |
else
|
|
|
101 |
{
|
|
|
102 |
return currencySymbol + ' ' + sign + integer + fractional + exponent;
|
|
|
103 |
}
|
|
|
104 |
//FIM DO CORPO DO PLUGIN
|
|
|
105 |
|
|
|
106 |
}
|
|
|
107 |
});
|