C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 21st, 2005, 08:18 PM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,655 fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 36 m 8 sec
Reputation Power: 32
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.

Reply With Quote
  #2  
Old November 21st, 2005, 08:29 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
This may not be exactly what you need, but have you tried things like

printf("a --> %8.2f ", a);

?

Reply With Quote
  #3  
Old November 21st, 2005, 09:12 PM
The Immortal ME The Immortal ME is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 136 The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
Send a message via AIM to 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.
__________________
- The Immortal ME

There are some who call me ... TIM?

Reply With Quote
  #4  
Old November 21st, 2005, 09:26 PM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,655 fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 36 m 8 sec
Reputation Power: 32
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.

Reply With Quote
  #5  
Old November 21st, 2005, 09:37 PM
The Immortal ME The Immortal ME is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 136 The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
Send a message via AIM to The Immortal ME
Cast it to an int to get rid of everything after the radiux.

printf ( "%5d", (int) fnum );

Reply With Quote
  #6  
Old November 21st, 2005, 09:40 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
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.

Reply With Quote
  #7  
Old November 21st, 2005, 09:55 PM
fletchsod's Avatar
fletchsod fletchsod is offline
I lov C in AIX/Linux, hate C++
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2003
Location: Jacksonville, Florida
Posts: 1,655 fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level)fletchsod User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 36 m 8 sec
Reputation Power: 32
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Printf() - float variables...

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap