CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to round the number to the nearest ten?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
emazzu630



Joined: 14 Aug 2018
Posts: 20
Location: Ouro Verde - SC

View user's profile Send private message

How to round the number to the nearest ten?
PostPosted: Wed Feb 27, 2019 11:49 pm     Reply with quote

With the button pressed (AP == 0), counting from 1 to 1 is performed with
the "UP and DOWN" buttons.
With the release button, (AP == 1), the count of 5 to 5 is performed
through the "UP and DOWN" buttons.
The point is that if the count of 1 to 1 is between 0 and 5 or between 5
and 10, the count of "5 to 5" is wrong.
A rounding is required that at the first pressing of the "UP and DOWN"
buttons, only the unit number will
"0" or "5" following the count of 5 to 5.
I declare the library "math.h" and tried unsuccessfully the
"value = ceil();" and "value = floor ();".
In the code below, I got the correct sequence on the display only to
appear when the button is pressed, when I release the number outside
the "0, 5, or 10" sequence appears again.
Anyone who has the solution, would you like to share? Thank you very much.
Code:

while((UP == 0)||(DOWN == 0)) {
   if (UP == 0)
      dados+=c;
   else
      dados-=c;
   7_seg(dados);
   d++;
   
   if ((AP == 1) &&
   ((dig0 == 1)||(dig0 == 2)||(dig0 == 3)||(dig0 == 4)||
   (dig0 == 6)||(dig0 == 7)||(dig0 == 8)||(dig0 == 9))) {
      if ((dig0 == 1)||(dig0 == 2)||(dig0 == 3)||(dig0 == 4)) {
         dig0 = 0; }
      else {
         dig0 = 5; }
      }
Ttelmah



Joined: 11 Mar 2010
Posts: 19529

View user's profile Send private message

PostPosted: Thu Feb 28, 2019 12:07 am     Reply with quote

What you are describing is 4/5 rounding. You do this with:

result=floor(val+4);

This gives 0 for 0,1,2,3,4, and then 5 for 5,6,7,8,9. Hence the '4/5'.

If you were working with floats, the formula is:

result=floor(val+4.99999);
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group