1b7696389b658409e58bc131b53b9d68b76a65c0
[platform/framework/web/crosswalk-tizen.git] /
1 # number #
2
3 Number utilities.
4
5
6 ## abbreviate(val[, nDecimalDigits, dictionary]):String
7
8 Abbreviate number to thousands (K), millions (M) or billions (B).
9
10 The default value for `nDecimalDigits` is `1`.
11
12 ### Example
13
14     abbreviate(123456);     // "123.5K"
15     abbreviate(12345678);   // "12.3M"
16     abbreviate(1234567890); // "1.2B"
17
18 You can set the amount of decimal digits (default is `1`):
19
20     abbreviate(543);    // "0.5K"
21     abbreviate(543, 1); // "0.5K"
22     abbreviate(543, 2); // "0.54K"
23     abbreviate(543, 3); // "0.543K"
24
25 You can customize the abbreviation by passing a custom "dictionary":
26
27     var _ptbrDict = {
28         thousands : ' mil',
29         millions : ' Mi',
30         billions : ' Bi'
31     };
32     function customAbbr(val) {
33         return abbreviate(val, 1, _ptbrDict);
34     }
35
36     customAbbr(123456); // "123.5 mil"
37     customAbbr(12345678); // "12.3 Mi"
38     customAbbr(1234567890); // "1.2 Bi"
39
40
41
42 ## currencyFormat(val[, nDecimalDigits, decimalSeparator, thousandsSeparator]):String
43
44 Format a number as currency.
45
46 ### Example:
47
48     currencyFormat(1000);              // "1,000.00"
49     currencyFormat(1000, 1);           // "1,000.0"
50     currencyFormat(1000, 2, ',', '.'); // "1.000,00"
51
52
53
54 ## enforcePrecision(val, nDecimalDigits):Number
55
56 Enforce a specific amount of decimal digits and also fix floating point
57 rounding issues.
58
59 ### Example:
60
61 ```js
62 enforcePrecision(0.615, 2); // 0.62
63 enforcePrecision(0.625, 2); // 0.63
64 //floating point rounding "error" (rounds to odd number)
65 +(0.615).toFixed(2);        // 0.61
66 +(0.625).toFixed(2);        // 0.63
67 ```
68
69
70 ## isNaN(val):Boolean
71
72 ES6 `Number.isNaN()`, checks if supplied value is `NaN`.
73
74 ```js
75 // only returns `true` for `NaN`
76 isNaN(NaN);    // true
77 isNaN(0 / 0);  // true
78
79 // everything else is `false`
80 isNaN(true);   // false
81 isNaN(123);    // false
82 isNaN('asd');  // false
83 isNaN('NaN');  // false
84 ```
85
86
87 ## MAX_INT:Number
88
89 Maximum 32-bit signed integer value. `Math.pow(2, 31) - 1`
90
91 ### Example:
92
93 ```js
94 console.log( MAX_INT ); // 2147483647
95 ```
96
97
98 ## MAX_SAFE_INTEGER:Number
99
100 Maximum safe integer. `Math.pow(2,53) − 1`
101
102
103 ## MAX_UINT:Number
104
105 Maximum 32-bit unsigned integer value. `Math.pow(2, 32) - 1`
106
107 ### Example:
108
109 ```js
110 console.log( MAX_UINT ); // 4294967295
111 ```
112
113
114 ## MIN_INT:Number
115
116 Minimum 32-bit signed integer value. `Math.pow(2, 31) * -1`.
117
118 ### Example:
119
120 ```js
121 console.log( MIN_INT ); // -2147483648
122 ```
123
124
125 ## nth(n):String
126
127 Returns the "nth" of number. (`"st"`, `"nd"`, `"rd"`, `"th"`)
128
129 ```js
130 nth(1); // "st"
131 nth(2); // "nd"
132 nth(12); // "th"
133 nth(22); // "nd"
134 nth(23); // "rd"
135 nth(34); // "th"
136 ```
137
138 See: [`ordinal()`](#ordinal)
139
140
141
142 ## ordinal(n):String
143
144 Converts number into ordinal form (1st, 2nd, 3rd, 4th, ...)
145
146 ```js
147 ordinal(1); // "1st"
148 ordinal(2); // "2nd"
149 ordinal(3); // "3rd"
150 ordinal(14); // "14th"
151 ordinal(21); // "21st"
152 ```
153
154 See: [`nth()`](#nth)
155
156
157
158 ## pad(n, minLength[, char]):String
159
160 Add padding zeros if `n.length` < `minLength`.
161
162 ### Example:
163
164 ```js
165 pad(1, 5);      // "00001"
166 pad(12, 5);     // "00012"
167 pad(123, 5);    // "00123"
168 pad(1234, 5);   // "01234"
169 pad(12345, 5);  // "12345"
170 pad(123456, 5); // "123456"
171
172 // you can also specify the "char" used for padding
173 pad(12, 5, '_'); // "___12"
174 ```
175
176 see: [string/lpad](./string.html#lpad)
177
178
179
180 ## rol(val, shift):Number
181
182 Bitwise circular shift left.
183
184 More info at [Wikipedia#Circular_shift](http://en.wikipedia.org/wiki/Circular_shift)
185
186
187
188 ## ror(val, shift):Number
189
190 Bitwise circular shift right.
191
192 More info at [Wikipedia#Circular_shift](http://en.wikipedia.org/wiki/Circular_shift)
193
194
195
196 ## sign(val):Number
197
198 Returns `-1` if value is negative, `0` if the value is `0` and `1` if value is positive. Useful for
199 multiplications.
200
201 ```js
202 sign(-123); // -1
203 sign(123);  // 1
204 sign(0);    // 0
205 ```
206
207
208
209 ## toInt(val):Number
210
211 "Convert" value into an 32-bit integer.  Works like `Math.floor` if `val > 0` and
212 `Math.ceil` if `val < 0`.
213
214 **IMPORTANT:** val will wrap at [number/MIN_INT](#MIN_INT) and
215 [number/MAX_INT](#MAX_INT).
216
217 Created because most people don't know bitwise operations and also because this
218 feature is commonly needed.
219
220 [Perf tests](http://jsperf.com/vs-vs-parseint-bitwise-operators/7)
221
222 ### Example:
223
224 ```js
225 toInt(1.25);   // 1
226 toInt(0.75);   // 0
227 toInt(-0.55);  // 0
228 toInt(-5.0001) // -5
229 ```
230
231
232
233 ## toUInt(val):Number
234
235 "Convert" value into an 32-bit unsigned integer.
236
237 Works like AS3#uint().
238
239 **IMPORTANT:** val will wrap at 2^32.
240
241 ### Example:
242
243 ```js
244 toUInt(1.25);                 // 1
245 toUInt(0.75);                 // 0
246 toUInt(-0.55);                // 0
247 toUInt(-5.0001);              // 4294967291
248 toUInt(Math.pow(2,32) - 0.5); // 4294967295
249 toUInt(Math.pow(2,32) + 0.5); // 0
250 ```
251
252
253 ## toUInt31(val):Number
254
255 "Convert" value into an 31-bit unsigned integer (since 1 bit is used for sign).
256
257 Useful since all bitwise operators besides `>>>` treat numbers as signed
258 integers.
259
260 **IMPORTANT:** val will wrap at 2^31 and negative numbers will be treated as
261 `zero`.
262
263 ### Example:
264
265 ```js
266 toUInt31(1.25);                 // 1
267 toUInt31(0.75);                 // 0
268 toUInt31(-0.55);                // 0
269 toUInt31(-5.0001);              // 0
270 toUInt31(Math.pow(2,31) - 0.5); // 21474836470
271 toUInt31(Math.pow(2,31) + 0.5); // 0
272 ```
273
274
275 -------------------------------------------------------------------------------
276
277 For more usage examples check specs inside `/tests` folder. Unit tests are the
278 best documentation you can get...
279