|
|
View previous topic :: View next topic |
Author |
Message |
Eldert Besseling
Joined: 07 Oct 2003 Posts: 7
|
visualisation float |
Posted: Thu Feb 10, 2005 2:52 am |
|
|
Hello,
What is the best way to print a negative value between -1 and 0.
I have the value -0.5 so i print it as:
printf( "%01.1f%", data );
the output is: -.5
i tried several possibilities to print the right float (such as: %2.1f e.g.)
but nothing helps.
Is there another way to print the right value? |
|
|
Ttelmah Guest
|
|
Posted: Thu Feb 10, 2005 3:39 am |
|
|
There are two parts to this. The first is to get the format right, and the second is to hope that CCS handles it correctly!. Neither of the formats you post, would be 'correct', for a leading '0'. The number to the right of the DP, is the 'precision', while the number left of the DP, is the 'minimum field width', or the number of digits after the DP for a float. Now in the first case, you have specified a 'width' of only one digit, and this is filled with the '5', so there is no leading 0. In the second example, you have lost the 'leading zero' flag.
So the supposedly 'correct' format, would be:
printf("%02.1f",data);
Note that you do not want the trailing % sign (this tells the compiler that the next character is another control sequence). If you want to display the '%' symbol after the numbers, you need '%%'.
However the CCS compiler (unfortunately), has some problems with handling leading zeros correctly. This varies with compiler versions, so if yours does not work properly, you would have to bodge round, possibly by printing the data to a string and adding the zero, or by testing for the number being ngative, and printing the '-0', then printing the -ve value for the rest of the data...
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
|