Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round number up to the nearest multiple of 3

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

like image 384
dotty Avatar asked Jul 15 '10 09:07

dotty


People also ask

How do you round to the nearest multiple?

The MROUND function rounds a number to the nearest given multiple. The multiple to use for rounding is provided as the significance argument. If the number is already an exact multiple, no rounding occurs and the original number is returned. You can...

How do I round up to the nearest multiple in Excel?

Excel MROUND function Number - the value you want to round. Multiple - the multiple to which you want to round the number. For example, the formula =MROUND(7, 2) rounds 7 to the nearest multiple of 2 and returns 8 as the result.


2 Answers

    if(n > 0)         return Math.ceil(n/3.0) * 3;     else if( n < 0)         return Math.floor(n/3.0) * 3;     else         return 3; 
like image 170
Cambium Avatar answered Sep 28 '22 01:09

Cambium


Simply:

3.0*Math.ceil(n/3.0) 

?

like image 25
Iwan B. Avatar answered Sep 28 '22 03:09

Iwan B.