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 support@ccsinfo.com

how to printf a floating number in the format of 00.00 ?

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



Joined: 30 Sep 2004
Posts: 14

View user's profile Send private message Send e-mail

how to printf a floating number in the format of 00.00 ?
PostPosted: Tue Nov 29, 2005 5:13 am     Reply with quote

How can I force the floating variables to be displayed in the format of say 00.00 for example ?

I am trying to send some variables down the RS232, some of which are integers and some of which are floating numbers.

The integers are displayed but the floating values move about and overwrite other text on the screen.

I am using CCS Serial Port Monitor to display the values.

The printf statement I am using is....
Code:

printf("Off=%02lu\t On=%02lu\t %%=%03U\t Delay=%05lu\t",FreqTriacOff,FreqTriacOn,TriacPercent,TriacDelay);
printf("Hz=%02f\t\t T.F=%02f\r",Period_Hz,TransferFunction);



A typical sample of the text displayed in CCS Serial Port Monitor is shown below...

Code:

Off=15   On=30   %=048   Delay=58809    Hz=22.300722     T.F=6.6 T.F=6.666666


See where the spacing has screwed up at the end of the line where is reads T.F=6.6 T.F=6.666666 (it should just read T.F=6.666666)

In the printf statement I tried to force the format of the floating numbers so that it would have 2 leading zeros by using the syntax %02f, but that doesnt work.

For example, in the typical sample shown above the value for T.F is 6.66666 so this should be displayed in the format of 06.6666

But it isnt !

Help !!
Ttelmah
Guest







PostPosted: Tue Nov 29, 2005 5:30 am     Reply with quote

The first number in a printf, is the _total field width_. A zero in front of this, signifies to pre-fill with zeros. There is a decimal point seperator allowed to specify the part after the DP.
To get what you want (two trailing zeros), in a field with two leading zeros, you need:

"%05.2f"

I'd possibly suggest using:

"%05.2g"

which will round the last digit, rather than just truncating.
This gives a minimum of a five character field (2 digits each side of the DP), filled with zero in front, and with 2 digits after the DP.

Best Wishes
bertronicom



Joined: 17 Nov 2004
Posts: 26
Location: University of Cantabria -SPAIN

View user's profile Send private message Send e-mail MSN Messenger

working with this
PostPosted: Tue Nov 29, 2005 10:28 am     Reply with quote

fprintf(CH1,"V %3.2f L %3.2f \r\n",V1, Lenght1);

Try to code this, it's working now
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