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 support@ccsinfo.com

Rounding numbers down

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



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

Rounding numbers down
PostPosted: Thu May 29, 2008 1:13 pm     Reply with quote

Hi guys, some of you were born already knowing the C language, while I started learning it - because of the PIC MCU's - when I reached 41... Smile

So here is my question and I hope that somebody can give me an answer to this soon:

The MCU is a 12F629 PIC.

The thing which I want to achieve is to round a number down. For example:

I have a variable which is calculated to be = 1522. I need to round this - always down to 1500. The variable is defined as an int16.

So what I was thinking to do this easily is to divide the number by 100, which should give me 15.22, but since the variable is defined as an INT it will loose the decimals, automatically giving me a 15 as a result. At this point I could multiply the variable back with 100 to get the straight 1500 as a result.

Will it be like this, or some kind of unknown (by me) buffer will keep in mind the decimals and when I multiply it back it will be the same as the original number?

Thanks.
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Thu May 29, 2008 1:41 pm     Reply with quote

Should work as you describe - no secret buffers Smile

Ken
RArtz



Joined: 28 May 2008
Posts: 7

View user's profile Send private message

PostPosted: Thu May 29, 2008 1:56 pm     Reply with quote

thats how ive done it in the past.
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Thu May 29, 2008 3:48 pm     Reply with quote

How about y= x-(x%100),

1500 = 1522 - (1522 % 100)

1522 % 100 = 22
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