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 2003Posts: 20404

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:

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

XavierGuest

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.HamlettGuest

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 2003Posts: 20404

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