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 CCS Technical Support

Printf lcd_putc question

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



Joined: 17 May 2007
Posts: 5

View user's profile Send private message

Printf lcd_putc question
PostPosted: Wed Jul 02, 2008 12:51 am     Reply with quote

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
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

PostPosted: Wed Jul 02, 2008 3:27 am     Reply with quote

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...
Ttelmah
Guest







PostPosted: Wed Jul 02, 2008 4:01 am     Reply with quote

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
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