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 CCS Technical Support

print using %f

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



Joined: 20 Aug 2009
Posts: 4

View user's profile Send private message

print using %f
PostPosted: Thu Aug 20, 2009 12:11 pm     Reply with quote

Hi
I have to send a float number [voltage_var variable contents] over rs232 using the next print sentence:
Code:

float voltage_var;
.
printf("Current voltage =%2.2f\r\n",voltage_var);
.

It works but I need my output string always same length, I mean if I have
1 in voltage var then I need to send "01.XX" NOT "1.XX" as actually it does !

How can I achieve this ?

Thanks by any help !
betomax



Joined: 20 Aug 2009
Posts: 4

View user's profile Send private message

PostPosted: Thu Aug 20, 2009 12:31 pm     Reply with quote

I tried also:

Code:
printf("Current voltage =%02.2f\r\n",voltage_var);


but still sending "Current voltage = 1.XX"
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 20, 2009 12:41 pm     Reply with quote

Make a test program and experiment with it. The first number in "%2.2"
is the minimum width of the printf output. The 2nd number is the number
of digits to the right of the decimal point. You can also put a "0" in front
of the first digit to zero-fill it on the left side, where applicable.

Take the code below, and change the width specifier (the first number)
until you get what you want. Use the "UART1" feature of the MPLAB
simulator to easily do these experiments without having to program
a hardware board each time.
Code:

#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main()
{
float voltage_var = 1.23;
 
printf("Current voltage =%2.2f\r\n",voltage_var);

while(1);
}


See this post for instructions on how to use the "UART1" feature of the
MPLAB simulator to display serial output in the MPLAB Output Window:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
betomax



Joined: 20 Aug 2009
Posts: 4

View user's profile Send private message

PostPosted: Thu Aug 20, 2009 1:11 pm     Reply with quote

PCM programmer wrote:
You can also put a "0" in front
of the first digit to zero-fill it on the left side, where applicable.




Thanks by reply, and yes actually I'm trying under mplab sim, but I don't understand what's wrong !

with
Code:
printf("Current voltage =%05.2f\r\n",voltage_var);


I got finally
Code:
Current voltage = 01.23


BUT it doesn't work when the voltage var is negative, I set voltage variable to -1.23 and then mplab outputs is:
Code:
Current voltage = -1.23


I can deal with this comparing before printing, but it is a good idea ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 20, 2009 1:19 pm     Reply with quote

It works if you change the field width to "6". Then you get -01.23
but if you want a constant field width, you will have to check if the
value is negative before calling the appropriate printf statement.
betomax



Joined: 20 Aug 2009
Posts: 4

View user's profile Send private message

PostPosted: Thu Aug 20, 2009 1:53 pm     Reply with quote

Quote:
you will have to check if the
value is negative before calling the appropriate printf statement.


Is exactly what I did, Thanks it works perfect !!! Smile
Ttelmah
Guest







PostPosted: Thu Aug 20, 2009 3:07 pm     Reply with quote

Also, a search would have found 'why' the original format didn't work.
In C (not just CCS), the number in front of the decimal point, is the _total field width_. Then the number after the decimal, is the digits to be placed after the decimal, _in this field_. The decimal, is included in the field width calculations.
Hence the need for the 05, to handle 2 digits in front of the decimal, and two after.

Best Wishes
minoan_war
Guest







PostPosted: Wed Sep 09, 2009 9:31 am     Reply with quote

good point Ttelmah
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