View previous topic :: View next topic |
Author |
Message |
oxxyfx
Joined: 24 May 2007 Posts: 97
|
Rounding numbers down |
Posted: Thu May 29, 2008 1:13 pm |
|
|
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...
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
|
|
Posted: Thu May 29, 2008 1:41 pm |
|
|
Should work as you describe - no secret buffers
Ken |
|
|
RArtz
Joined: 28 May 2008 Posts: 7
|
|
Posted: Thu May 29, 2008 1:56 pm |
|
|
thats how ive done it in the past. |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Thu May 29, 2008 3:48 pm |
|
|
How about y= x-(x%100),
1500 = 1522 - (1522 % 100)
1522 % 100 = 22 |
|
|
|