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() problem displaying float value without decimal plac

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



Joined: 20 Sep 2004
Posts: 14
Location: Sacramento, CA

View user's profile Send private message

printf() problem displaying float value without decimal plac
PostPosted: Mon Mar 28, 2005 10:16 pm     Reply with quote

I am trying to get printf() to output a 2 digit display value from a float value and have not had success thus far getting it to behave. The float variable has a value between 0 and 70 and I only want to display ' 0', '70', '35' and similar such concepts.

Normally I would use something like this if I wanted ' 0.1', '70.0', '35.4':

Code:
printf ("%04.1f",fVariable);


Thus I would assume I could use this for my desired goal:

Code:
printf ("%02f",fVariable);

No such luck. Other combinations including "%2.0f", "%02.0f", "%3.0f" do no yield the desired effect either.

Any suggestions? I can always convert to an Int and then display but I would think that the native printf() function SHOULD just be able to do this properly.

For reference I'm working on an 18F8720 and I'm on version 3.214 of the compiler.
Ttelmah
Guest







PostPosted: Tue Mar 29, 2005 2:24 am     Reply with quote

%3.0f, should have given the required effect. There do seem to be some compiler 'issues', with the way the conversion arithmetic is done, leading to tiny errors, but I have just tried it, and %3.0f, for a float 'val', counting from 1 to 10, gives 1,2,3,4,5,6,7,8,9,10 on 3.221.
There is though a leading space, since this is the default for numbers. It may be that you need the %-3.0f format, which moves the numbers tight to the left of the output field, rather than giving this space, which unfortunately, CCS don't support in their version of printf...
You can simulate the effect, by using sprintf to a string buffer, and then printf to output the result from one character right. So:

int8 b[6];
sprintf(b,"%3.0f",val);
printf("%s",b+1);

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