Thread: format specifier

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    format specifier

    Hi all,
    I want to output numbers in scientific notation eg 1.2345E-06, but I want the number to start with "0." eg 0.12345E-05.
    I cannot find any option for the format specifier and all I can think of is to convert the first part to string (eg 1.2345), add a "0." on the printf statement, and then add 1 (integer) to the second part after the E (eg -06+1).

    This is far from elegant way of doing this, so do you have any recomendations on any other way?

    Thanks

    Spiros

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The only way to do that is to write the number out and then maybe the number will be 0.1234...

    In correct scientific notation, the first digit must be nonzero, so there is no reason you should need to worry about this.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    34
    Try this.
    Code:
    char holder[MAX_PATH];
    float i=//Make it equal what ever you want to scientfic notation.//
    sprintf(holder,"%E",i);
    //Also you can make the E small doing this.
    sprintf(holder,"%e",i); same thing different case
    //Example output//
    3.9265e2 or
    	3.9265E2
    Last edited by unkownname; 05-12-2006 at 05:03 PM.
    Its Really All up to you.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    integers do not work that way. Use a double.
    Code:
     double num = 1.456e-5;
    printf("%e\n", num);
    But this writes a float in scientific notation. The %f identifier writes floats and doubles out.
    Code:
    double num = 1.456e-5;
    printf("%f\n", num);
    Last edited by whiteflags; 05-12-2006 at 05:25 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    34
    Oh Alright it was just a shot I never tried it out.
    Its Really All up to you.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    thanks guys, I'll try your suggestions.
    I am fully aware that this notation is not correct (I'm doing a PhD in mechanical engneering/maths...), but there is a program (in FORTRAN) that I am using which uses a large database of numbers. In there, every number starts with "0." and because I am adding numbers to that database, I just want to be consistent with how these numbers are presented.

    Spiros

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. format specifier of size_t
    By broli86 in forum C Programming
    Replies: 2
    Last Post: 07-01-2008, 10:22 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. system command not executing with format specifier
    By ridhamshah in forum C Programming
    Replies: 6
    Last Post: 11-08-2006, 05:58 AM
  4. string format specifier
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 07-31-2006, 04:43 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM