LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-09-2004, 08:05 AM   #1
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
C Output Question


Using:
printf("%f\n", 4.3)
I get a result like 4.300000. Is there any way to automatically realise that it should be 4.3, and print that? Hopefully quite a fast way. I know that it can be done with many sprintfs, and looking through the strings and working out where the zero's start, and then using those strings instead, but I was hoping there would be an easier way.
 
Old 09-09-2004, 08:26 AM   #2
Quis
Member
 
Registered: May 2001
Location: Germany, Süd-Baden
Distribution: Gentoo
Posts: 139

Rep: Reputation: 15
you can do this simply with changing:
printf("%f\n", 4.3);

to

printf("%1f\n", 4.3)
 
Old 09-09-2004, 09:58 AM   #3
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
That doesn't seem to make any difference.

Last edited by drigz; 09-09-2004 at 10:00 AM.
 
Old 09-09-2004, 10:07 AM   #4
Quis
Member
 
Registered: May 2001
Location: Germany, Süd-Baden
Distribution: Gentoo
Posts: 139

Rep: Reputation: 15
sorry i have forgotten a small dot:

printf("%.1f\n", 4.3)



the devil is in the detail

der teufel steckt im detail
 
Old 09-09-2004, 01:28 PM   #5
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
That appears to round it to 1 decimal place, so 4 is printed as 4.0, and 4.32 is printed as 4.3 - I need a way which will automatically cut off any excess 0's, so 4 would be 4, 4.3 would be 4.3 and 4.32 would be 4.32. Thank you.
 
Old 09-09-2004, 01:44 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Code:
#include <stdio.h>

char *round(double num)
{
  static char buf[100];
  int i;

  snprintf(buf, sizeof(buf), "%f", num);
  for(i = 0;buf[i];++i)
    if(buf[i] == '0')
      break;
  buf[i] = '\0';
  return buf;
}

int main(void)
{
  printf("%s\n", round(4.3));
  printf("%s\n", round(4.32));
  printf("%s\n", round(4.0));
  printf("%s\n", round(4.999));

  return 0;
}
Quote:
itsme@dreams:~/C$ ./round
4.3
4.32
4.
4.999
 
Old 09-09-2004, 01:48 PM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
I should read my program's output more carefully. Here's a fixed round() function that does what you need it to:
Code:
char *round(double num)
{
  static char buf[100];
  int i;

  snprintf(buf, sizeof(buf), "%f", num);
  // Find '.'
  for(i = 0;buf[i] && buf[i] != '.';++i);  // Whole loop
  // Clear any trailing 0's
  for(i += 2;buf[i] && buf[i] != '0';++i);  // Whole loop
  buf[i] = '\0';
  return buf;
}
Quote:
itsme@dreams:~/C$ ./round
4.3
4.32
4.0
4.999
 
Old 09-09-2004, 02:12 PM   #8
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
I get undefined reference to _snprintf when doing this... This normally means that I'm not compiling with the right libraries - which ones do I need (I'm using DJGPP under windows)?

Also - I'll fix these myself if there's a problem - does 4 print 4 or 4.0? does 4.302 print right?
 
Old 09-09-2004, 02:22 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
4 works, but 4.302 doesn't. DJGPP might not have snprintf() because it should be in the standard C library. You can just use sprintf() since %f won't show over 100 characters anyway.

One last try here:
Code:
#include <stdio.h>

char *round(double num)
{
  static char buf[100];
  int i;

  sprintf(buf, "%f", num);
  // Get to the end of the string
  for(i = 0;buf[i];i++); // Whole loop
  // Work back until the first non-zero character
  for(i--;buf[i] == '0';i--);  // Whole loop
  if(buf[i] == '.')
    i++;
  buf[i+1] = '\0';
  return buf;
}

int main(void)
{
  printf("%s\n", round(4.3));
  printf("%s\n", round(4.32));
  printf("%s\n", round(4.0));
  printf("%s\n", round(4.999));
  printf("%s\n", round(4));
  printf("%s\n", round(4.302));

  return 0;
}
Quote:
itsme@dreams:~/C$ ./round
4.3
4.32
4.0
4.999
4.0
4.302
itsme@dreams:~/C$
There
 
Old 09-09-2004, 03:30 PM   #10
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
It seems that now, whenever I output two rounded figures in short succession, the second number is equal to the first... here is the relevant part of the code I am using:

Code:
typedef struct complex {
	double a;
	double b;
};

struct complex stored[30];
int nstored=0;

int store(double a, double b){
	stored[nstored].a=a;
	stored[nstored].b=b;
	display(nstored);
	return(nstored++);
}

int display(int z){
	if (stored[z].b < 0)
		printf("%d: %s - %sj\n", z, round(stored[z].a), round(stored[z].b*(-1)) );
	else
		printf("%d: %s + %fj\n", z, round(stored[z].a), stored[z].b );
}
And the relevant output:
Code:
store 4 5
0: 4 + 5.000000j
store 4 -5
1: 4 - 4j
Ones with positive b are outputted normally to show the difference - I know the problem in the printing, because it knows that b is < 0, but not that is it 5....

NB: I have changed the round function so that it outputs 4 instead of 4.0 (changed an i++ to i--)
 
Old 09-09-2004, 04:08 PM   #11
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
It's because round() is using a static buffer. The problem could be fixed with dynamic memory allocation, but the calling function would have to remember to free the memory. As it is now you just have to seperate calls to round() into different statements.
 
Old 09-09-2004, 04:35 PM   #12
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
thats really really annoying... if i take away the static, would my memory just fill up with a new 100 char string every time i run round? how would i go about clearing them?
 
Old 09-10-2004, 02:50 AM   #13
rustynailz
Member
 
Registered: Jun 2004
Distribution: MDK 9.2/10.0, VectorLinux 4.0
Posts: 50

Rep: Reputation: 15
printf ("%g\n", 4.302): 4.302
printf ("%g\n", 4.30200000): 4.302
printf ("%g\n", 4.0): 4

Should do the trick.
 
Old 09-10-2004, 07:32 AM   #14
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
perfect - that's exactly what i was looking for!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about output of dmesg command. sarajevo Linux - Security 3 09-09-2005 03:44 AM
iwconfig output question scowles Linux - Wireless Networking 2 12-03-2004 05:11 PM
Stupid question... too much output in terminal Darx Linux - Newbie 4 04-15-2004 09:20 AM
Firewalling OUTPUT chain question Gilion Linux - Networking 0 10-16-2003 03:50 AM
please help, simple question about redirect output kool Linux - General 4 03-18-2002 09:34 AM


All times are GMT -5. The time now is 03:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration