LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Output Question (https://www.linuxquestions.org/questions/programming-9/c-output-question-228542/)

drigz 09-09-2004 08:05 AM

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.

Quis 09-09-2004 08:26 AM

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

to

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

drigz 09-09-2004 09:58 AM

That doesn't seem to make any difference.

Quis 09-09-2004 10:07 AM

sorry i have forgotten a small dot:

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



the devil is in the detail

der teufel steckt im detail

drigz 09-09-2004 01:28 PM

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.

itsme86 09-09-2004 01:44 PM

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

itsme86 09-09-2004 01:48 PM

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

drigz 09-09-2004 02:12 PM

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?

itsme86 09-09-2004 02:22 PM

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 :)

drigz 09-09-2004 03:30 PM

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--)

itsme86 09-09-2004 04:08 PM

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.

drigz 09-09-2004 04:35 PM

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?

rustynailz 09-10-2004 02:50 AM

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

Should do the trick.

drigz 09-10-2004 07:32 AM

perfect - that's exactly what i was looking for!


All times are GMT -5. The time now is 01:05 PM.