Use PHP functions in JavaScript

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);
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

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

q  I wonder how you got so good. HaHa ! This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your design is so perfect ! You certainly know how to get a girls attention ! I’m glad that you’re here. I feel like I’ve learned something new by being here

Gravatar
Rafał Kukawski
30 Jun '11 Permalink

q  @dude: Math.floor(value) !== value|value
floor(-10.5) === -11
(-10.5|-10.5) === -10
Your approach is only valid for positive numbers.

Gravatar
dude
30 Jun '11 Permalink

q  again... DO IT RIGHT!!!
return value|value

Gravatar
Brett Zamir
18 Feb '11 Permalink

q  @ejrdtykufg: Good joke, but if you weren't aware of the site's purpose, read the comments below yours.

Gravatar
ejrdtykufg
17 Feb '11 Permalink

q  Please add author's portrait to this function!

Gravatar
Brett Zamir
9 Dec '10 Permalink

q  @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.

Gravatar
Andrew
9 Dec '10 Permalink

q  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.

Gravatar
Kevin van Zonneveld
8 Sep '10 Permalink

q  @ 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

Gravatar
Brett Zamir
6 Sep '10 Permalink

q  @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.

Gravatar
Mogria
5 Sep '10 Permalink

q  I think this function is unnecessary.
Why you don't use "Math.floor" ...


Contribute a New function

More functions

In this category

abs
acos
acosh
asin
asinh
atan
atan2
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
» floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log
log10
log1p
max
min
mt_getrandmax
mt_rand
octdec
pi
pow
rad2deg
rand
round
sin
sinh
sqrt
tan
tanh

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !