JavaScript floor
Returns the next lowest integer value from the number
1 2 3 4 56 7 8 9 10 | function floor (value) { // Returns the next lowest integer value from the number // // version: 1109.2015 // discuss at: http://phpjs.org/functions/floor // + original by: Onno Marsman // * example 1: floor(8723321.4); // * returns 1: 8723321 return Math.floor(value); } |
Examples
Running
1 | floor(8723321.4); |
Should return
1 | 8723321 |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have floor goodness in JavaScript.
@dude: Math.floor(value) !== value|value
floor(-10.5) === -11
(-10.5|-10.5) === -10
Your approach is only valid for positive numbers.
@ejrdtykufg: Good joke, but if you weren't aware of the site's purpose, read the comments below yours.
@Andrew: Yes, that works well, but 1) We're following the same functional format for the sake of familiarity to our users (and possibly also simplicity in our compiler). 2) All our functions can be used not only in a non-namespaced environment, but also in a namespaced one (which I know could be done by "floor: Math.floor" if the compiler were aware of this format). Granted, in that case, one may just as well use Math.floor directly, but as we describe somewhere in our FAQ or site, some functions are implemented just for the sake of students of JavaScript (coming from PHP backgrounds) or for those still just functioning as script kiddies who are rushing to put something together that works, and who haven't taken time yet to learn the details of the language they will hopefully eventually learn more deeply. That being said, the large majority of functions in php.js are not and cannot be mere wrappers for the limited utility set built into JavaScript since PHP is far more expressive as far as built-in utilities.
Why don't you just have
floor = Math.floor;
Has the same functionality and it's marginally faster in all browsers too, since it bypasses the encumbered 'Math' object.
@ Mogria: This can also be used as a learning reference for PHP folks trying to learn JavaScript. Besides this opens up all kinds of opportunities like the Harmony Framework
@Mogria: You can and probably should just use Math.floor. We are trying to fully implement the PHP API, both for the sake of convenience of those familiar with PHP and moving to JavaScript, and for the sake of (usually) offering convenient features that do not always map one-to-one to a built-in JavaScript function as this one does.


أخبار أقتصاد و أعمال
Apr 18th