This week's book giveaway is in the OCAJP forum.We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# how to find out the multiples of 12?

senthil sen
Ranch Hand
Posts: 184
say a user enter a value ---
that value is lesser than multiples of 12 then it should be rounded to that multiple of 12
eg-
user value --- 22
multiple valueof 12 --24

the user value should be rounded to 24
if user value entered is ---35
multiple value of 12 --- 36
the user value should be rounded to 36
how can i do this??

Peter den Haan
author
Ranch Hand
Posts: 3252
To round up: ((value+11)/12)*12
Proper rounding: ((value+6)/12)*12
To round down: (value/12)*12
- Peter

Greg Brouelette
Ranch Hand
Posts: 144
Don't forget the modulus operator %
int x = 15;
int y = x%12;
y will be the remainder of 15/12 In this case "3". If y is 0 then it is divisible by 12. Other wise you know how far away you are from a multiple of 12 and can round up or down as required.

senthil sen
Ranch Hand
Posts: 184
thanks guys for ur reply,well it works if there is an input below 12
what if the user selects 19
i should be able to check it out and find if its is near to a multiple to 12
eg; 19 is near to 24
29 is near 24
31 is near 36
40 is near 36
44 is near 48

how to check these options??
this all happens when a user selects from the user input?
say a user wanna buy apples...he selects these..

Barry Gaunt
Ranch Hand
Posts: 7729
Peter gave you this formula: ((value+6)/12)*12 try putting your numbers (value) in that. Do not forget that "/" is integer division (without remainder).
[ February 21, 2003: Message edited by: Barry Gaunt ]