double format specification

Discussion in 'C++' started by JK, Jun 9, 2008.

  1. JK

    JK Guest

    The following is a sample program I wrote..

    float fTick = 0.0001;
    float fLotSize = 100;
    int iDeno = 10000;

    double dVal;
    double dInt;

    dInt = fTick * fLotSize;
    dVal = fTick * fLotSize * iDeno;

    printf ("dVal : %g dInt : %g fTick: %g\n", dVal, dInt, fTick);

    This is printing the output as :

    dVal : 100 dInt : 0.01 fTick: 1e-04

    If I use the format type "f" instead of "g" it prints the following
    output

    dVal : 99.999997 dInt : 0.010000 fTick: 0.000100

    Here is my doubt. I always want the output in dddd.dddd ( where
    dddd is one more decimal digits. It can go upto 10)
    What should I give as the format specification to print the ouput in
    the dddd.dddd format ?
    Please help to resolve this problem..
    FYI: I use g++ compiler (linux-gnu-2.4).
     
    JK, Jun 9, 2008
    #1
    1. Advertising

  2. JK

    Guest

    On Jun 9, 3:13 pm, JK <> wrote:
    > The following is a sample program I wrote..
    >
    > float fTick = 0.0001;
    > float fLotSize = 100;
    > int iDeno = 10000;
    >
    > double dVal;
    > double dInt;
    >
    > dInt = fTick * fLotSize;
    > dVal = fTick * fLotSize * iDeno;
    >
    > printf ("dVal : %g dInt : %g fTick: %g\n", dVal, dInt, fTick);
    >
    > This is printing the output as :
    >
    > dVal : 100 dInt : 0.01 fTick: 1e-04
    >
    > If I use the format type "f" instead of "g" it prints the following
    > output
    >
    > dVal : 99.999997 dInt : 0.010000 fTick: 0.000100
    >
    > Here is my doubt. I always want the output in dddd.dddd ( where
    > dddd is one more decimal digits. It can go upto 10)
    > What should I give as the format specification to print the ouput in
    > the dddd.dddd format ?
    > Please help to resolve this problem..
    > FYI: I use g++ compiler (linux-gnu-2.4).


    The printf is a C language function and not a C++ function. Use cout
    instead.
     
    , Jun 9, 2008
    #2
    1. Advertising

  3. wrote:
    ...
    > The printf is a C language function and not a C++ function. Use cout
    > instead.


    printf is still part of C++ as well. Still, it is better to use
    std::eek:stream.
     
    Gianni Mariani, Jun 9, 2008
    #3
  4. JK

    Kai-Uwe Bux Guest

    wrote:

    > On Jun 9, 3:13 pm, JK <> wrote:
    >> The following is a sample program I wrote..
    >>
    >> float fTick = 0.0001;
    >> float fLotSize = 100;
    >> int iDeno = 10000;
    >>
    >> double dVal;
    >> double dInt;
    >>
    >> dInt = fTick * fLotSize;
    >> dVal = fTick * fLotSize * iDeno;
    >>
    >> printf ("dVal : %g dInt : %g fTick: %g\n", dVal, dInt, fTick);
    >>
    >> This is printing the output as :
    >>
    >> dVal : 100 dInt : 0.01 fTick: 1e-04
    >>
    >> If I use the format type "f" instead of "g" it prints the following
    >> output
    >>
    >> dVal : 99.999997 dInt : 0.010000 fTick: 0.000100
    >>
    >> Here is my doubt. I always want the output in dddd.dddd ( where
    >> dddd is one more decimal digits. It can go upto 10)


    Huh? I don't understand what you mean by "one more decimal digits"? One more
    than what? Can you provide examples of the desired formatting?


    >> What should I give as the format specification to print the ouput in
    >> the dddd.dddd format ?
    >> Please help to resolve this problem..
    >> FYI: I use g++ compiler (linux-gnu-2.4).

    >
    > The printf is a C language function


    correct.

    > and not a C++ function.


    incorrect. The printf family is part of C++ and required to be provided by a
    conforming implementation.

    > Use cout instead.


    In order to make that work as a response to the OP, you should at least give
    some hints as to how the desired formatting can be achieved using cout and
    its companions (just in case that you, unlike me, actually understand which
    formatting the OP wants:).


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 9, 2008
    #4
  5. JK

    Guest

    On Jun 9, 5:09 pm, Kai-Uwe Bux <> wrote:
    > wrote:
    > > On Jun 9, 3:13 pm, JK <> wrote:
    > >> The following is a sample program I wrote..

    >
    > >> float fTick = 0.0001;
    > >> float fLotSize = 100;
    > >> int iDeno = 10000;

    >
    > >> double dVal;
    > >> double dInt;

    >
    > >> dInt = fTick * fLotSize;
    > >> dVal = fTick * fLotSize * iDeno;

    >
    > >> printf ("dVal : %g dInt : %g fTick: %g\n", dVal, dInt, fTick);

    >
    > >> This is printing the output as :

    >
    > >> dVal : 100 dInt : 0.01 fTick: 1e-04

    >
    > >> If I use the format type "f" instead of "g" it prints the following
    > >> output

    >
    > >> dVal : 99.999997 dInt : 0.010000 fTick: 0.000100

    >
    > >> Here is my doubt. I always want the output in dddd.dddd ( where
    > >> dddd is one more decimal digits. It can go upto 10)

    >
    > Huh? I don't understand what you mean by "one more decimal digits"? One more
    > than what? Can you provide examples of the desired formatting?
    >
    > >> What should I give as the format specification to print the ouput in
    > >> the dddd.dddd format ?
    > >> Please help to resolve this problem..
    > >> FYI: I use g++ compiler (linux-gnu-2.4).

    >
    > > The printf is a C language function

    >
    > correct.
    >
    > > and not a C++ function.

    >
    > incorrect. The printf family is part of C++ and required to be provided by a
    > conforming implementation.
    >
    > > Use cout instead.

    >
    > In order to make that work as a response to the OP, you should at least give
    > some hints as to how the desired formatting can be achieved using cout and
    > its companions (just in case that you, unlike me, actually understand which
    > formatting the OP wants:).
    >
    > Best
    >
    > Kai-Uwe Bux


    Agreed :)

    JK, Please see this link: http://www.arachnoid.com/cpptutor/student3.html

    Regards
    Shan
     
    , Jun 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gaurav
    Replies:
    0
    Views:
    16,181
    Gaurav
    Nov 7, 2003
  2. andy
    Replies:
    0
    Views:
    4,174
  3. Sydex
    Replies:
    12
    Views:
    6,601
    Victor Bazarov
    Feb 17, 2005
  4. Harish Sundararaj

    Information/specification -- LXT2 format

    Harish Sundararaj, Jul 25, 2006, in forum: VHDL
    Replies:
    1
    Views:
    1,363
  5. Robert Dailey

    Bug in format specification in Python 3?

    Robert Dailey, Aug 11, 2009, in forum: Python
    Replies:
    3
    Views:
    374
Loading...

Share This Page