views:

470

answers:

5
+5  Q: 

Round Down Number

How can I round down a number in Javascript?

math.round() doesn't work because it rounds it to the nearest decimal.

I'm not sure if there is a better way of doing it other than breaking it apart at the decimal point at keeping the first bit. There must be...

+20  A: 
Math.floor()

is the answer.

phoebus
Really? I googled like loads of stuff. And it didn't come up. Strange to have such a simple answer that google can't find.
Ben Shelock
It's also the slowest method; if you need to perform a lot of these, use the bitwise | operator (see my post).
The | operator won't work on numbers larger than 2147483647.
Robert L
@Ben Shelock: Google can certainly find it: http://www.google.com/search?q=javascript+floor - it's just a classic case of knowing what to look for. :)
Greg Hewgill
@Greg: Yeah, once you know what you're looking for, it's easy to find!
masher
@Greg: Yeah, I didn't know what it was called.
Ben Shelock
A: 

So you want to truncate a double?

One way is to assign it to an int (or long).

double d = 12.3456;
int i = d; //i = 12
masher
Double d :)
Nissan Fan
What javascript are you using ;)
oops, fell into java mode... :)
masher
Suggest you delete this answer as it's not relevant.
Jason S
A: 
Math.floor(1+7/8)
DigitalRoss
1+7/8 = 1 - Not much need for Math.floor() there :)
Jason Berry
Actually it's (7/8)+1 which is not 1. Thank you 3rd grade algebra
Joe Philllips
Umm, please actually try this in a javascript program. I did. Display (1 + 7/8) and you will see 1.875. Math.round(...) is 2, Math.floor(...) is 1. What are you guys talking about?
DigitalRoss
Or open the Firefox Error Console. Or Firebug. It isn't hard to try. I tried it. 1 + 7/8 is 1.875 in js. Did you possibly forget that all math in js is in floating point?
DigitalRoss
It's probably easy to forget that javascript does everything in floating point. In many *other* languages 1+7/8 is 1, but in js it really is 1.875.
DigitalRoss
+1  A: 

Math.floor() will work, but it's very slow compared to using a bitwise OR operation:

var rounded = 34.923 | 0;
alert( rounded );
//alerts "34"

EDIT Math.floor() is not slower than using the | operator. Thanks to Jason S for checking my work.

Here's the code I used to test:

var a = [];
var time = new Date().getTime();
for( i = 0; i < 100000; i++ ) {
    //a.push( Math.random() * 100000  | 0 );
    a.push( Math.floor( Math.random() * 100000 ) );
}
var elapsed = new Date().getTime() - time;
alert( "elapsed time: " + elapsed );
??? I just ran jsdb (www.jsdb.org) which uses Spidermonkey 1.7, and ran a loop to sum up the floor'ed value of x[i] on an array of 100000 floating point numbers, first with Math.floor(), then with bitwise or as you suggest. It took approx the same time, 125 msec.
Jason S
Just repeated the test with 500000 floating point numbers, it took approx the same time, approx 625 msec.
Jason S
So I don't see how 1.25usec is very slow.
Jason S
Can't argue with your data :) I think I may be have confused JS's implementation with ActionScript's (built on EcmaScript; obviously implementation differs). Thanks for checking my work!
@Jason S: I don't think .5s is slow to do things, but it doesn't mean you shouldn't aim to be as quick as possible if you have the opportunity.
Dominic Bou-Samra
+5  A: 

Round towards negative infinity - Math.Floor()

+3.5 => +3.0
-3.5 => -4.0

Round towards zero - usually called Truncate(), but not supported by JavaScript. Can be emulated by using Math.Ceil() for negative numbers and Math.Floor() for positive numbers.

+3.5 => +3.0 using Math.Floor()
-3.5 => -3.0 using Math.Ceil()
Daniel Brückner