Page 1 of 1
Print a long duble
Posted: Sat Apr 01, 2006 11:32 pm
by d31mOZ
How i can print a variable "long double", for example:
Code: Select all
#include <iostream.h>
int main()
{
long double n1 = 100000000;
cout << n1;
return 0;
}
The program is good, but print:
1e+08
I want that print:
100000000
What i can do it?
Greetings...
Posted: Sun Apr 02, 2006 10:17 am
by andresw1
Hello,
You can use the stdio.h instead of iostream. With stdio.h you can print long double in the following way:
long double d = 100.0;
printf("%Lf",d); -> This wil print something like 100.00000000
To cut the 0's in the end you can modify the %lf in the following way
printf("%.3Lf",d); -> The number after the dot is how many digits you want to print after the hole part.
If someone knows hoe to do it with iostram I would also be intrested to understand... Now when I have doubles in the problem I use stdio, otherwise iostream...
[Edit] "Lf" instead of "lf" after misof noticed my mistake. I'm sorry.
Posted: Sun Apr 02, 2006 1:36 pm
by misof
The correct format string for a "long double" is "%Lf", not "%lf". The format string "%lf" is for "double".
About doing it using streams, you really should consider using Google. E.g., try the query "cout fixed format".
Posted: Sun Apr 02, 2006 3:30 pm
by Krzysztof Duleba
It's easy when you know that "fixed" is the keyword. However, even simple search at C++ board for iostream double returns the answer.