mozilla

Compare Revisions

Math.round()

Change Revisions

Revision 350623:

Revision 350623 by crumpled on

Revision 363841:

Revision 363841 by haasz on

Title:
Math.round
Math.round
Slug:
JavaScript/Reference/Global_Objects/Math/round
JavaScript/Reference/Global_Objects/Math/round
Tags:
"Math", "Math Methods", "JavaScript"
"Math", "Math Methods", "JavaScript"
Content:

Revision 350623
Revision 363841
tt85    <h3 id="Example:_Decimal_rounding" name="Example:_Decimal_rou
 >nding">
86      Example: Decimal rounding
87    </h3>
88    <pre class="brush:js">
89// Closure
90(function(){
91 
92        /**
93         * Decimal adjustment of a number.
94         *
95         * @param       {String}        type    The type of adjus
 >tment.
96         * @param       {Number}        value   The number.
97         * @param       {Integer}       exp             The expon
 >ent (the 10 logarithm of the adjustment base).
98         * @returns     {Number}                        The adjus
 >ted value.
99         */
100        function decimalAdjust(type, value, exp) {
101                // If the exp is undefined or zero...
102                if (typeof exp === 'undefined' || +exp === 0) {
103                        return Math[type](value);
104                }
105                value = +value;
106                exp = +exp;
107                // If the value is not a number or the exp is not
 > an integer...
108                if (isNaN(value) || !(typeof exp === 'number' &am
 >p;&amp; exp % 1 === 0)) {
109                        return NaN;
110                }
111                // Shift
112                value = value.toString().split('e');
113                value = Math[type](+(value[0] + 'e' + (value[1] ?
 > (+value[1] - exp) : -exp)));
114                // Shift back
115                value = value.toString().split('e');
116                return +(value[0] + 'e' + (value[1] ? (+value[1] 
 >+ exp) : exp));
117        }
118 
119        // Decimal round
120        if (!Math.round10) {
121                Math.round10 = function(value, exp) {
122                        return decimalAdjust('round', value, exp)
 >;
123                };
124        }
125        // Decimal floor
126        if (!Math.floor10) {
127                Math.floor10 = function(value, exp) {
128                        return decimalAdjust('floor', value, exp)
 >;
129                };
130        }
131        // Decimal ceil
132        if (!Math.ceil10) {
133                Math.ceil10 = function(value, exp) {
134                        return decimalAdjust('ceil', value, exp);
135                };
136        }
137 
138})();
139 
140// Round
141Math.round10(55.55, -1); // 55.6
142Math.round10(55.549, -1); // 55.5
143Math.round10(55, 1); // 60
144Math.round10(54.9, 1); // 50
145Math.round10(-55.55, -1); // -55.5
146Math.round10(-55.551, -1); // -55.6
147Math.round10(-55, 1); // -50
148Math.round10(-55.1, 1); // -60
149// Floor
150Math.floor10(55.59, -1); // 55.5
151Math.floor10(59, 1); // 50
152Math.floor10(-55.51, -1); // -55.6
153Math.floor10(-51, 1); // -60
154// Ceil
155Math.ceil10(55.51, -1); // 55.6
156Math.ceil10(51, 1); // 60
157Math.ceil10(-55.59, -1); // -55.5
158Math.ceil10(-59, 1); // -50
159</pre>

Back to History