]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datatype/datatype-number-format.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datatype / datatype-number-format.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-format', function(Y) {
9
10 /**
11  * Number submodule.
12  *
13  * @module datatype
14  * @submodule datatype-number
15  */
16
17 /**
18  * Format number submodule.
19  *
20  * @module datatype
21  * @submodule datatype-number-format
22  */
23  
24 /**
25  * DataType.Number provides a set of utility functions to operate against Number objects.
26  *
27  * @class DataType.Number
28  * @static
29  */
30 var LANG = Y.Lang;
31
32 Y.mix(Y.namespace("DataType.Number"), {
33      /**
34      * Takes a Number and formats to string for display to user.
35      *
36      * @method format
37      * @param data {Number} Number.
38      * @param config {Object} (Optional) Optional configuration values:
39      *  <dl>
40      *   <dt>prefix {String}</dd>
41      *   <dd>String prepended before each number, like a currency designator "$"</dd>
42      *   <dt>decimalPlaces {Number}</dd>
43      *   <dd>Number of decimal places to round. Must be a number 0 to 20.</dd>
44      *   <dt>decimalSeparator {String}</dd>
45      *   <dd>Decimal separator</dd>
46      *   <dt>thousandsSeparator {String}</dd>
47      *   <dd>Thousands separator</dd>
48      *   <dt>suffix {String}</dd>
49      *   <dd>String appended after each number, like " items" (note the space)</dd>
50      *  </dl>
51      * @return {String} Formatted number for display. Note, the following values
52      * return as "": null, undefined, NaN, "".
53      */
54     format: function(data, config) {
55         if(LANG.isNumber(data)) {
56             config = config || {};
57
58             var isNeg = (data < 0),
59                 output = data + "",
60                 decPlaces = config.decimalPlaces,
61                 decSep = config.decimalSeparator || ".",
62                 thouSep = config.thousandsSeparator,
63                 decIndex,
64                 newOutput, count, i;
65
66             // Decimal precision
67             if(LANG.isNumber(decPlaces) && (decPlaces >= 0) && (decPlaces <= 20)) {
68                 // Round to the correct decimal place
69                 output = data.toFixed(decPlaces);
70             }
71
72             // Decimal separator
73             if(decSep !== "."){
74                 output = output.replace(".", decSep);
75             }
76
77             // Add the thousands separator
78             if(thouSep) {
79                 // Find the dot or where it would be
80                 decIndex = output.lastIndexOf(decSep);
81                 decIndex = (decIndex > -1) ? decIndex : output.length;
82                 // Start with the dot and everything to the right
83                 newOutput = output.substring(decIndex);
84                 // Working left, every third time add a separator, every time add a digit
85                 for (count = 0, i=decIndex; i>0; i--) {
86                     if ((count%3 === 0) && (i !== decIndex) && (!isNeg || (i > 1))) {
87                         newOutput = thouSep + newOutput;
88                     }
89                     newOutput = output.charAt(i-1) + newOutput;
90                     count++;
91                 }
92                 output = newOutput;
93             }
94
95             // Prepend prefix
96             output = (config.prefix) ? config.prefix + output : output;
97
98             // Append suffix
99             output = (config.suffix) ? output + config.suffix : output;
100
101             return output;
102         }
103         // Not a Number, just return as string
104         else {
105             return (LANG.isValue(data) && data.toString) ? data.toString() : "";
106         }
107     }
108 });
109
110
111 }, '3.3.0' ,{requires:['yui-base']});