double format specification

J

JK

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

shan_rish

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.
 
G

Gianni Mariani

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.
 
K

Kai-Uwe Bux

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?

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
 
S

shan_rish

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?



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


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top