Formatting Scientific Notation output

This is a discussion on Formatting Scientific Notation output within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> // Example program............. copywright protected, 2003 int main() { float data; data = 0.023; printf ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Formatting Scientific Notation output

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    // Example program............. copywright protected, 2003
    int main()
    {
    	float data;
    
    	data = 0.023;
    
                    printf ("e = %e\n",data);
    
    	return 0;
    }
    for my result I am getting.

    e = 2.300000e-002

    I would like to get 2.3e-002

    but if my data is... data = 0.023900
    I want to get 2.39e-002, not 2.390000e-002

    How to set the %e formatter so I don't get any trailing zeros.

    Thanks.
    Last edited by mashley; 02-07-2003 at 04:11 PM.
    Go Angels!
    Go Lakers!
    Go Trojans!
    Go Stock Market!

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    From the C that I know, you can't do that with an int. You could save each number as a string of chars (using itoa() and changing the way you input data), then just search for a '0' to stop displaying digits.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    printf( "e = %.2e\n", data );

    will get you 2.39e-002
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Originally posted by kurz7
    printf( "e = %.2e\n", data );

    will get you 2.39e-002
    But if I hardcode the %.2e in there, it will only work for numbers with 2 significant digits after the decimal point. If it has 3, 4 or 5 etc..., it will get chopped off. I don't want that. I just want to get rid of all trailing zeroes, no matter how many significant digits past the decimal point. There has to be a way to do that with a different %e format...

    VR,

    mashley

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    bump
    Go Angels!
    Go Lakers!
    Go Trojans!
    Go Stock Market!

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>There has to be a way to do that with a different %e format...
    Nope, %e prints extra 0's according to the precision of the data type that you're printing. So if the type has 6 digits of precision and you only fill 2 of them, %e gives you 4 0's. You can get around that by finding out what the precision is that you fill and doing this
    Code:
    printf("%.*e\n", precision, data);
    Or you can take more control over your data by converting it to a string and changing what you need to. The biggest problem with the previous solution is finding what the precision should be for the %e format. The next solution doesn't have that problem :-)
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void print_e(float value)
    {
      char *s;
      char *p;
      char  buff[50] = {0};
    
      /* Stringize so we can work with it */
      sprintf(buff, "%e", value);
    
      /* Copy everything except 0's up to the e */
      for (s = p = buff; (*p = *s) != 'e'; s++)
      {
        if (*s != '0')
        {
          p++;
        }
      }
    
      /* Copy everything after the e */
      while ((*p = *s) != '\0')
      {
        p++;
        s++;
      }
    
      /* It's printed nicely :-) */
      printf("%s\n", buff);
    }
    
    int main()
    {
      print_e(0.023f);
      print_e(0.023900f);
      
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. formatting output
    By z.tron in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2002, 05:11 PM
  4. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21