The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Printf() - float variables...
Discuss Printf() - float variables... in the C Programming forum on Dev Shed. Printf() - float variables... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 21st, 2005, 08:18 PM
|
 |
I lov C in AIX/Linux, hate C++
|
|
Join Date: Jul 2003
Location: Jacksonville, Florida
|
|
|
Printf() - float variables...
I haven't figured out how to do this. What I have here is a double variable. But I can't get it to be displayed in 5 precision.
Code:
double a, b, c;
a = 1.0;
b = 2348.34;
c = 23.0000003;
printf("a --> %5g", a);
printf("b --> %5g", b);
printf("c --> %5g", c);
Let the * be a space. What I want the this value without the decimal part to be ....
a to be "****1"
b to be "*2348"
c to be "***23"
I tried the "G", "g", etc... but that doesn't work. I would appreciated it if you can explain what I did wrong and how to fix it.
|

November 21st, 2005, 08:29 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
This may not be exactly what you need, but have you tried things like
printf("a --> %8.2f ", a);
?
|

November 21st, 2005, 09:12 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 136
  
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
|
|
|
You are specifying the minimum field width (numberof spaces on taken up by the number, even if they are blank), not the precision. The precision is specified using a '.' followed by the numberof digits.
__________________
- The Immortal ME
There are some who call me ... TIM?
|

November 21st, 2005, 09:26 PM
|
 |
I lov C in AIX/Linux, hate C++
|
|
Join Date: Jul 2003
Location: Jacksonville, Florida
|
|
Quote: | Originally Posted by The Immortal ME You are specifying the minimum field width (numberof spaces on taken up by the number, even if they are blank), not the precision. The precision is specified using a '.' followed by the numberof digits. |
Yea that minimum field width while chopping off that decimal point and anything after it.
|

November 21st, 2005, 09:37 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 136
  
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
|
|
|
Cast it to an int to get rid of everything after the radiux.
printf ( "%5d", (int) fnum );
|

November 21st, 2005, 09:40 PM
|
 |
Contributing User
|
|
|
|
Code:
#include <stdio.h>
int main(void)
{
double a, b, c;
a = 1.0;
b = 2348.34;
c = 23.0000003;
printf("a --> [%5.0f]\n", a);
printf("b --> [%5.0f]\n", b);
printf("c --> [%5.0f]\n", c);
return 0;
}
/* my output
a --> [ 1]
b --> [ 2348]
c --> [ 23]
*/
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
|

November 21st, 2005, 09:55 PM
|
 |
I lov C in AIX/Linux, hate C++
|
|
Join Date: Jul 2003
Location: Jacksonville, Florida
|
|
|
That simple!! Boy, must have gotten so confuse with the teacher's demostration about things like..
%8.3f --> where it would be ****.***
%2.5f --> where it would be I dunno. :-)
Thanks.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|