|
|
View previous topic :: View next topic |
Author |
Message |
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
float printf() problem (Solved) |
Posted: Sat Aug 25, 2012 9:08 pm |
|
|
The statement printf () does not work correctly if I try to send a float in this format:
% 02.2f
if the value to be displayed is 2.02, the value sent by printf () is 2.02, when the expected value is 02.02
The only way to send the cipher is using this format:
% 02.0f
But it does not help me as I need to send the data in whole and decimal positions 00.00.
Can someone explain what would be the solution?
I tried it with the 4.124 version and also in the 4.134 version, with the same result, not to do to solve it ... _________________ MGLSOFT
Last edited by mglsoft on Sun Aug 26, 2012 12:40 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun Aug 26, 2012 1:22 am |
|
|
You are misunderstanding the printf command (as do a lot of people). The number in front of the decimal, when printing, is _not_ the number of digits in front of the decimal in the result, but the _total output width_. So you are telling the command to give a leading zero (correct), in a field 2 digits wide, with two digits after the decimal point in the result. This means the number will always be overloading the field width.
The decimal point is also counted as an output character, so the correct syntax for two digits after the decimal, and two digits in front, with leading zeros, is:
"%05.2f"
This is standard C, and is very commonly 'got wrong'...
Best Wishes |
|
|
mglsoft
Joined: 18 Feb 2008 Posts: 48
|
Float Printf() now OK !! |
Posted: Sun Aug 26, 2012 8:15 am |
|
|
I tell the truth, I did not expect such a quick response and accurate.
Whenever I read the help and manuals CCS compiler, and as much as I reread, I find that this explained so well and with so few words, on how to format output printf () in the way you already.
Thank you very much for your help, as it has saved my programming day.
I will be careful when a query you, to try to return this attention, but I suspect that their level of knowledge about C is well above the average of the forum, and is also well above the mine.
It is a pleasure to learn from someone like you, who also allowed to provide this knowledge to others.
It is tested and works properly.
Thank you very much.
Thanks Google for the translation from the Spanish. _________________ MGLSOFT |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun Aug 26, 2012 11:37 am |
|
|
Thank you.
Flag the thread as 'solved'.
Best Wishes |
|
|
|
|
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
|