Asked : Nov 17

Viewed : 35 times

I'd like to round at most 2 decimal places, but *only if necessary*.

Input:

```
10
1.7777777
9.1
```

Output:

```
10
1.78
9.1
```

How can I do this in JavaScript?

Nov 17

Use `Math.round()`

:

```
Math.round(num * 100) / 100
```

Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :

`Math.round((num + Number.EPSILON) * 100) / 100`

answered Jan 16

Keep type as integer for later sorting or other math operations:

`Math.round(1.7777777 * 100)/100`

1.78

```
// Round up!
Math.ceil(1.7777777 * 100)/100
```

1.78

```
// Round down!
Math.floor(1.7777777 * 100)/100
```

1.77

Or convert to string:

`(1.7777777).toFixed(2)`

"1.77"

answered Jan 16

`Math.round((num + Number.EPSILON) * 100) / 100`

answered Jan 16