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 a floating point number

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



Joined: 10 Feb 2004
Posts: 205

View user's profile Send private message

printf a floating point number
PostPosted: Fri Apr 30, 2004 1:09 pm     Reply with quote

I have a float number 1245.1223344

I want to use printf to display it like:

1245.1

I also want leading zeroes:

0045.2

I tried:

printf ("%06f",fltnum);

But this doesn't do it. It prints too many decimal places.


Answer:

printf ("%06.1f,flnum);
Ttelmah
Guest







Re: printf a floating point number
PostPosted: Fri Apr 30, 2004 2:58 pm     Reply with quote

ljbeng wrote:
I have a float number 1245.1223344

I want to use printf to display it like:

1245.1

I also want leading zeroes:

0045.2

I tried:

printf ("%06f",fltnum);

But this doesn't do it. It prints too many decimal places.


Answer:

printf ("%06.1f,flnum);

You want a total of five digits, with one after the decimal. The syntax for this, is:
%05.1f
Some versions of the CCS compiler have problems with this type of format, and the arithmetic for printf, is done in floating point, which makes it relatively bulky (and slow). You may get a 'better' result with:

ldiv_t temp;
temp=ldiv(flnum,10);
printf("%04ld.%1ld",temp.quot,temp.rem);

This calculates the long integer quotient and remainder from a division by 10, and then prints these as two seperate integers.

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