![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
rblue Guest
|
Float Values in Printf() upto 2 decimal values |
Posted: Wed Apr 04, 2007 4:25 am |
|
|
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
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Apr 04, 2007 4:56 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
rblue Guest
|
|
Posted: Wed Apr 04, 2007 5:46 am |
|
|
well thanx buddy its working with
temp = x/4.0:
and %3.2f |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|