Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

# Compare Revisions

## Math.floor()

Change Revisions

### Revision 316227:

Revision 316227 by ethertank on

### Revision 363847:

Revision 363847 by haasz on

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

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

Back to History