dotty - 4 months ago 5x

Javascript Question

Hay, how would i go about rounded a number up the nearest multiple of 3?

ie

`25 would return 27`

1 would return 3

0 would return 3

6 would return 6

thanks

Answer

```
if(n > 0)
return Math.ceil(n/3.0) * 3;
else if( n < 0)
return Math.floor(n/3.0) * 3;
else
return 3;
```

Source (Stackoverflow)

Comments