# JavaScript Math Reference

# Basic Rounding Functions

`Math.floor(x)` -&gt; Always rounds DOWN to nearest integer

```js
Math.floor(4.9); // 4
Math.floor(4.1); // 4
Math.floor(-4.1); // -5 (down means more negative)
Math.floor(-4.9); // -5
```

`Math.ceil(x)` -&gt; Always rounds UP to nearest integer

```js
Math.ceil(4.1); // 5
Math.ceil(4.9); // 5
Math.ceil(-4.9); // -4 (up means less negative)
Math.ceil(-4.1); // -4
```

`Math.round(x)` -&gt; Rounds to nearest integer (0.5 rounds up)

```js
Math.round(4.4); // 4
Math.round(4.5); // 5
Math.round(4.6); // 5
Math.round(-4.5); // -4
Math.round(-4.6); // -5
```

`Math.trunc(x)` -&gt; Removes decimal part (truncates toward zero)

```js
Math.trunc(4.9); // 4
Math.trunc(4.1); // 4
Math.trunc(-4.9); // -4 (toward zero)
Math.trunc(-4.1); // -4
```

# Rounding to Decimal Places

**Round to N decimal places:**

```js
// Round to 2 decimal places
Math.round(4.567 * 100) / 100; // 4.57
Number((4.567).toFixed(2)); // 4.57

// Round to 1 decimal place
Math.round(4.567 * 10) / 10; // 4.6
```

# Quick Reference

| Function | 4.2 | 4.7 | \-4.2 | \-4.7 |
| --- | --- | --- | --- | --- |
| `floor` | 4 | 4 | \-5 | \-5 |
| `ceil` | 5 | 5 | \-4 | \-4 |
| `round` | 4 | 5 | \-4 | \-5 |
| `trunc` | 4 | 4 | \-4 | \-4 |

**Memory trick:** Floor goes down, ceiling goes up, truncate cuts off decimals, round goes to nearest.
