%e format question

This is a discussion on %e format question within the C Programming forums, part of the General Programming Boards category; Hi, I got such a quick (and good) response to my last question that I thought I'd post another that's ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    8

    %e format question

    Hi,

    I got such a quick (and good) response to my last question that I thought I'd post another that's been bugging me.

    When printing a number in exponential format (%e) using printf or one of its friends, three digits are used for the exponent:

    1.2345e+006.

    I would like to change this behavior so that the exponent only uses as many digits as it needs:

    1.2345e+6

    I've looked, but this feature does not appear to be built into any of the format modifiers/switches. Am I missing something and/or is there an easy way to do this?

    Thnx,
    Scott

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I'm not seeing it, the man page from printf says that there are always at least two digits after the 'e'. If it is important you could always use string manipulation (sprintf onto a char array, search for the 'e', remove all zeros after it and shift the remaining digits left, then print the resultant string)

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    8
    Originally posted by Imperito
    I'm not seeing it, the man page from printf says that there are always at least two digits after the 'e'.
    Yea, noticed that too. In my implimentation (gcc), though, it's always 3. Maybe it's 2 for floats--dunno 'cus everthing I'm working with is double precision.

    If it is important you could always use string manipulation (sprintf onto a char array, search for the 'e', remove all zeros after it and shift the remaining digits left, then print the resultant string)
    That was all I could come up with too--just hoping I'd missed a more elegant approach.

    Thanks for the reply.

    Scott

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Program format Question
    By deadhippo in forum C Programming
    Replies: 0
    Last Post: 05-13-2008, 10:33 PM
  3. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  4. Can't Format C
    By caroundw5h in forum Tech Board
    Replies: 40
    Last Post: 04-26-2004, 09:57 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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