]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datatype/datatype-number.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datatype / datatype-number.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('datatype-number-parse', function(Y) {
9
10 /**
11  * Parse number submodule.
12  *
13  * @module datatype
14  * @submodule datatype-number-parse
15  * @for DataType.Number
16  */
17
18 var LANG = Y.Lang;
19
20 Y.mix(Y.namespace("DataType.Number"), {
21     /**
22      * Converts data to type Number.
23      *
24      * @method parse
25      * @param data {String | Number | Boolean} Data to convert. The following
26      * values return as null: null, undefined, NaN, "".
27      * @return {Number} A number, or null.
28      */
29     parse: function(data) {
30         var number = (data === null) ? data : +data;
31         if(LANG.isNumber(number)) {
32             return number;
33         }
34         else {
35             return null;
36         }
37     }
38 });
39
40 // Add Parsers shortcut
41 Y.namespace("Parsers").number = Y.DataType.Number.parse;
42
43
44 }, '3.3.0' ,{requires:['yui-base']});
45 YUI.add('datatype-number-format', function(Y) {
46
47 /**
48  * Number submodule.
49  *
50  * @module datatype
51  * @submodule datatype-number
52  */
53
54 /**
55  * Format number submodule.
56  *
57  * @module datatype
58  * @submodule datatype-number-format
59  */
60  
61 /**
62  * DataType.Number provides a set of utility functions to operate against Number objects.
63  *
64  * @class DataType.Number
65  * @static
66  */
67 var LANG = Y.Lang;
68
69 Y.mix(Y.namespace("DataType.Number"), {
70      /**
71      * Takes a Number and formats to string for display to user.
72      *
73      * @method format
74      * @param data {Number} Number.
75      * @param config {Object} (Optional) Optional configuration values:
76      *  <dl>
77      *   <dt>prefix {String}</dd>
78      *   <dd>String prepended before each number, like a currency designator "$"</dd>
79      *   <dt>decimalPlaces {Number}</dd>
80      *   <dd>Number of decimal places to round. Must be a number 0 to 20.</dd>
81      *   <dt>decimalSeparator {String}</dd>
82      *   <dd>Decimal separator</dd>
83      *   <dt>thousandsSeparator {String}</dd>
84      *   <dd>Thousands separator</dd>
85      *   <dt>suffix {String}</dd>
86      *   <dd>String appended after each number, like " items" (note the space)</dd>
87      *  </dl>
88      * @return {String} Formatted number for display. Note, the following values
89      * return as "": null, undefined, NaN, "".
90      */
91     format: function(data, config) {
92         if(LANG.isNumber(data)) {
93             config = config || {};
94
95             var isNeg = (data < 0),
96                 output = data + "",
97                 decPlaces = config.decimalPlaces,
98                 decSep = config.decimalSeparator || ".",
99                 thouSep = config.thousandsSeparator,
100                 decIndex,
101                 newOutput, count, i;
102
103             // Decimal precision
104             if(LANG.isNumber(decPlaces) && (decPlaces >= 0) && (decPlaces <= 20)) {
105                 // Round to the correct decimal place
106                 output = data.toFixed(decPlaces);
107             }
108
109             // Decimal separator
110             if(decSep !== "."){
111                 output = output.replace(".", decSep);
112             }
113
114             // Add the thousands separator
115             if(thouSep) {
116                 // Find the dot or where it would be
117                 decIndex = output.lastIndexOf(decSep);
118                 decIndex = (decIndex > -1) ? decIndex : output.length;
119                 // Start with the dot and everything to the right
120                 newOutput = output.substring(decIndex);
121                 // Working left, every third time add a separator, every time add a digit
122                 for (count = 0, i=decIndex; i>0; i--) {
123                     if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
124                         newOutput = thouSep + newOutput;
125                     }
126                     newOutput = output.charAt(i-1) + newOutput;
127                     count++;
128                 }
129                 output = newOutput;
130             }
131
132             // Prepend prefix
133             output = (config.prefix) ? config.prefix + output : output;
134
135             // Append suffix
136             output = (config.suffix) ? output + config.suffix : output;
137
138             return output;
139         }
140         // Not a Number, just return as string
141         else {
142             return (LANG.isValue(data) && data.toString) ? data.toString() : "";
143         }
144     }
145 });
146
147
148 }, '3.3.0' ,{requires:['yui-base']});
149
150
151 YUI.add('datatype-number', function(Y){}, '3.3.0' ,{use:['datatype-number-parse', 'datatype-number-format']});
152