# math – How can I round down a number in Javascript?

## math – How can I round down a number in Javascript?

Using `Math.floor()`

is one way of doing this.

More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

Round towards negative infinity – `Math.floor()`

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

Round towards zero can be done using `Math.trunc()`

. Older browsers do not support this function. If you need to support these, you can use `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()
```

#### math – How can I round down a number in Javascript?

`Math.floor()`

will work, but its 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.

Heres 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 );
```