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

Float Values in Printf() upto 2 decimal values

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







Float Values in Printf() upto 2 decimal values
PostPosted: Wed Apr 04, 2007 4:25 am     Reply with quote

hi every one ..
i am trying to get float value upto 2 decimal places in C we do it like "%.2F" but as i use the same in ccs it gives an error Printf format(%)invalid...

like i want

Code:

int x =70;

float temp;
 temp=(x/4);
printf ("%.2f", temp);

but its not working any suggestion if can get in print f upto 2 decimal places like for this case i want out put

Code:

17.50
Ttelmah
Guest







PostPosted: Wed Apr 04, 2007 4:56 am     Reply with quote

First, beware this line:

temp=(x/4);

This would give problems in _any_ traditional C. You are evaluating 'x/4', using integer arithmetic, so will not get decimals in the result...
You need:

temp=(x/4.0);

or a 'cast' to convert 'x' to a floating point value, to get the FP result.

Now the %.2f statement, should be accepted OK. However there is a 'caveat' with it's operation. On the 'older' CCS compiler versions, if you use a %f format, without a 'width' specifier, you may get 'screwy' results. It is safer to use something like %6.2f.
Now, though the compiler is complaining about the %f format, it is quite common, for the compiler to error on one thing, when the real fault is elsewhere. Look carefully around. Have you got the #use RS232 statement setup and working?. Double check the line really is as typed?. etc. etc..
Also as a 'comment', be aware, that the compiler won't tidyly 'round' the value. So you may well get '17.49', rather than '17.50'.
If it still doesn't work, come back with the compiler version number. It compiles fine for me, with half a dozen versions ranging from 3.163, to the current V4.031 compiler.

Best Wishes
rblue
Guest







PostPosted: Wed Apr 04, 2007 5:46 am     Reply with quote

well thanx buddy its working with

temp = x/4.0:

and %3.2f
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