View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
signed 32 bit number with a decimal place? |
Posted: Sun Jul 19, 2015 9:34 pm |
|
|
Hi All,
How do i print a signed 32 bit number with a decimal place?
Thanks,
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jul 19, 2015 10:31 pm |
|
|
The CCS manual says %w is only for unsigned integers, but it appears to
work for signed. The program below has this output in MPLAB vs. 8.92
simulator:
Code: |
#include <18F4620.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//===================================
void main()
{
signed int32 num = -12345;
printf("%5.3w \r", num);
while(TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19492
|
|
Posted: Mon Jul 20, 2015 2:43 am |
|
|
It's one where they really should update the manual....
The function is very cleverly 'massively overloaded', and accepts int8, int16, int32, unsigned or signed, without demur. The impressive thing is that all appear to work OK.
It's been doing this for a long time now (there were some hiccups in some early V4 versions), but it 'does a lot more than it says on the tin'!. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jul 20, 2015 8:13 am |
|
|
That's odd... it was not working for me last night, thus my question.
I'll retry tonight and if all fails, I'll post a sample program.
I'm using PCH with the version on my signature.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19492
|
|
Posted: Mon Jul 20, 2015 8:22 am |
|
|
It's vital that the number you pass is correctly typed.
It'll only accept the integer as signed, if it is typed as signed, as PCM_Programmer shows.
I checked right back to quite early V4 versions a while ago, and all worked OK. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jul 20, 2015 2:35 pm |
|
|
Maybe more info is necessary.
My sensor returns a 32 bit int like: 2501 which i want to display as 25.01
It's a temperature sensor.
I've gotten it to display as 2501.00 but thats not really what I'm looking for.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 20, 2015 2:43 pm |
|
|
The program below shows 25.01 in MPLAB simulator with CCS vs. 5.047:
Code: | #include <18F4620.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//===================================
void main()
{
signed int32 num = 2501;
printf("%4.2w \r", num);
while(TRUE);
} |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jul 20, 2015 7:56 pm |
|
|
Well that worked and now i feel stupid... because i tried it last night...
Thanks for your help, ive done the changes as suggested and its working as expected.
thanks! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Mon Jul 20, 2015 8:10 pm |
|
|
There is nothing that fixes something faster than trying to show someone else what is broken
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19492
|
|
Posted: Tue Jul 21, 2015 12:15 am |
|
|
Very true.
It's often not even the actual 'showing', but the act of getting 'ready to show' makes you look again, and suddenly the answer becomes obvious. |
|
|
|