|
|
View previous topic :: View next topic |
Author |
Message |
Xavier Guest
|
Printf and float ? |
Posted: Wed Jan 22, 2003 3:17 pm |
|
|
Hello everybody,
Does anyone can help me with printf function with float value ?
My pb :
1) I would like to print float value with leading zeros
printf("\%05.1f",val);
the result is 23.7 with val = 23.7 and should be 023.7 ?
whats wrong ?
2) How can I force the sign with printf ?
printf("\%+05.1f",val); does'nt works ! why ?
Many thanks in advance for your help.
Regards,
Xavier
___________________________
This message was ported from CCS's old forum
Original Post ID: 10898 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Printf and float ? |
Posted: Wed Jan 22, 2003 6:30 pm |
|
|
:=Hello everybody,
:=Does anyone can help me with printf function with float value ?
:=
:=My pb :
:=
:=1) I would like to print float value with leading zeros
:=printf("\%05.1f",val);
:=the result is 23.7 with val = 23.7 and should be 023.7 ?
:=whats wrong ?
:=
:=2) How can I force the sign with printf ?
:=printf("\%+05.1f",val); does'nt works ! why ?
:=
------------------------------------------------------------
I used PCM vs. 3.136 to run the test program below,
and got the following results in the terminal window:
023.7
+023.7
#include "c:\program files\picc\devices\16F877.h"
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use Delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//===============================================
void main()
{
float val;
val = 23.7;
printf("\%05.1f\n\r",val);
printf("\n\r");
printf("+\%05.1f",val);
while(1);
}
In the last printf statement, I moved the "+" sign to
the beginning of the format string.
___________________________
This message was ported from CCS's old forum
Original Post ID: 10911 |
|
|
Xavier Guest
|
Re: Printf and float ? |
Posted: Thu Jan 23, 2003 3:39 am |
|
|
Hi,
Thanks for you help
I used PCM vs. 3.118
val = 23.7;
printf("\%05.1f\n\r",val);
The result is 23.7 with no leading zeros ?!
val = -23.7
printf("+\%05.1f",val);
The result is +-23.7 with no leading zeros and sign pb ?!
The aim is to format the result like
+002.1 if val=2.1
+279,5 if val=279.5
-123.4 if val=-123.4
-025.9 if vzl=-25.9
Any ideas ?
PS : how to purchase free upgrade of PCM ?
Thanks in advance
Regards,
Xavier
___________________________
This message was ported from CCS's old forum
Original Post ID: 10923 |
|
|
R.J.Hamlett Guest
|
Re: Printf and float ? |
Posted: Thu Jan 23, 2003 3:42 am |
|
|
:=Hi,
:=Thanks for you help <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0">
:=I used PCM vs. 3.118
:=
:=val = 23.7;
:=printf("\%05.1f\n\r",val);
:=The result is 23.7 with no leading zeros ?!
:=
:=val = -23.7
:=printf("+\%05.1f",val);
:=The result is +-23.7 with no leading zeros and sign pb ?!
:=
:=The aim is to format the result like
:=+002.1 if val=2.1
:=+279,5 if val=279.5
:=-123.4 if val=-123.4
:=-025.9 if vzl=-25.9
:=
:=Any ideas ?
:=
:=PS : how to purchase free upgrade of PCM ?
:=
:=Thanks in advance
:=Regards,
:=Xavier
Yes. This has been a running problem with CCS's printf. I am glad to see that it has now been fixed.
The solution on older compilers, is to 'bodge' it, by using sprintf to send the characters to a text buffer, and then add the zeros yourself. :-(
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 10925 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Printf and float ? |
Posted: Thu Jan 23, 2003 12:46 pm |
|
|
:=:=Hi,
:=:=Thanks for you help <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0">
:=:=I used PCM vs. 3.118
:=:=
:=:=val = 23.7;
:=:=printf("\%05.1f\n\r",val);
:=:=The result is 23.7 with no leading zeros ?!
:=:=
:=:=val = -23.7
:=:=printf("+\%05.1f",val);
:=:=The result is +-23.7 with no leading zeros and sign pb ?!
:=:=
:=:=The aim is to format the result like
:=:=+002.1 if val=2.1
:=:=+279,5 if val=279.5
:=:=-123.4 if val=-123.4
:=:=-025.9 if vzl=-25.9
:=:=
:=:=Any ideas ?
:=:=
:=:=PS : how to purchase free upgrade of PCM ?
:=:=
:=:=Thanks in advance
:=:=Regards,
:=:=Xavier
:=Yes. This has been a running problem with CCS's printf. I am glad to see that it has now been fixed.
:=The solution on older compilers, is to 'bodge' it, by using sprintf to send the characters to a text buffer, and then add the zeros yourself. :-(
:=
---------------------------------------------------------
Here's a post that will help him use the sprintf method.
<a href="http://www.pic-c.com/forum/general/posts/8305.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/8305.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10943 |
|
|
|
|
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
|