This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to find out the multiples of 12?

 
senthil sen
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic