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