![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
dasilvarsa
Joined: 17 May 2007 Posts: 5
|
Printf lcd_putc question |
Posted: Wed Jul 02, 2008 12:51 am |
|
|
Good morning Experts,
I need to print a floating point number to the lcd in a special way
If my number is 0.12789456 I need to have this number displayed as
.12789456 usion printf lcd_putc (no leading zero)
if the number is 1.12789456 I need it displayed as
.12789456
I have tried various methods and I always get the leading zero or one being printed.
Could someone give the correct syntax if this is possible.
Regards
Joe
Trainer |
|
![](templates/subSilver/images/spacer.gif) |
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
|
Posted: Wed Jul 02, 2008 3:27 am |
|
|
could try printing the float to a string, then search in the string for the "." the. only use the data from this point to the end of the string. haven't tried it but might work... |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Jul 02, 2008 4:01 am |
|
|
First comment, the last few digits are pointless. The standard arithmetic (unless you are on PCD), only gives about 6.5 digits of accuracy...
Easiest way, is probably to remove the digits in front of the decimal point before starting. So:
val = val - (int32)val;
With 'val' as the floating point value. This will take the integer part of 'val', and subtract this from the number, giving just the decimals.
This is not really 'necessary' with the code below, but will simplify things.
Then sprintf the number into a string buffer. So:
char buffer[11];
sprintf(buffer,"%10.8f",val);
Then just print the 'tail' of the buffer, from the decimal point. So:
printf("%s",strchr(string,".");
Now, this prints all of the string from the decimal point,so would not 'care' if there was stuff in front of the decimal. However there is a 'caveat'. The CCS compiler, has problems sometimes, when asked to output values using printf, that have significantly more digits than the maths really supports. You _may_ find problems, and have to reduce the number of decimals used.
I see Crystal Lattice has just posted the same basic technique, while I was typing. :-)
Best Wishes |
|
![](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
|