|
|
View previous topic :: View next topic |
Author |
Message |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
printf a floating point number |
Posted: Fri Apr 30, 2004 1:09 pm |
|
|
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 |
Posted: Fri Apr 30, 2004 2:58 pm |
|
|
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 |
|
|
|
|
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
|