This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 2 years ago.
I don't need to do any calculations just print out the numbers.
double n1 = 1000000.5985;
double n2 = 9999999.0;
double n3 = 678300.893;
When I try
cout << n1 << n2 << n3;
I get 1e+006 1e+007 678301
How do I get it to print the whole number without converting to a string?
Use setprecision() from <iomanip>.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double f = 1000000.5985;
cout << f << endl;
cout << fixed << setprecision(6) << f <<endl;
cout << fixed << setprecision(5) << f << endl;
cout << fixed << setprecision(9) << f << endl;
return 0;
}
Output is
1e+06
1000000.598500
1000000.59850
1000000.598500000
Related
I am trying to convert a string decimal number into a double, however when I use the atof() function, my number ends up rounding to the whole number.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
The output is:
135428
I want:
135427.7
cout does that, not atof().
More precisely, operator<<, which inserts the formated data into the std::ostream.
You can use std::setprecision() from the <iomanip> standard library to print the decimals:
cout << setprecision(7) << r << endl;
or
cout << fixed << setprecision(1) << r << endl;
If you want to print the whole 135427.7000:
cout << fixed << setprecision(4) << r << endl;
This question already has answers here:
Why do I get the same result with rand() every time I compile and run? [duplicate]
(4 answers)
Why does rand() yield the same sequence of numbers on every run?
(7 answers)
Closed 4 years ago.
I am using rand() to generate a random number and display it in the context of an arithmetic problem. I get the same two numbers everytime. How do I make these different each time?
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
int numberOne = rand()%1000;
int numberTwo = rand()%1000;
int numberThree = numberOne + numberTwo;
char input;
cout << " " << numberOne << endl;
cout << "+" << numberTwo << endl;
cout << "----";
cin.get();
cout << numberThree << endl;
}
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
/* initialize random seed: */
srand (time(NULL));
int numberOne = rand()%1000;
int numberTwo = rand()%1000;
int numberThree = numberOne + numberTwo;
char input;
cout << " " << numberOne << endl;
cout << "+" << numberTwo << endl;
cout << "----";
cin.get();
cout << numberThree << endl;
}
In order for you to get a random number you need to provide a seed.
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 5 years ago.
Whenever i write 5 as n and p as 2 ,i get the output as 24...please let me know what's wrong? for other numbers it is completely fine.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
double n, p;
cout << "enter the number" <<endl;
cin >> n;
cout << "enter the power" <<endl;
cin >> p;
int result = pow(n, p);
cout << "Result is " << result;
return 0;
}
You have problems with your data types!
double n, p; // here you use double types
std::cout << "enter the number" << std::endl;
std::cin >> n;
std::cout << "enter the power" << std::endl;
std::cin >> p;
int result = pow(n, p); // then here you use double pow(double, double) but assign it to an int
std::cout << "Result is " << std::result;
Solution:
double result = pow(n, p);
This question already has answers here:
Correct use of std::cout.precision() - not printing trailing zeros
(3 answers)
Closed 6 years ago.
I am trying to print double and integers as double. For doing so, I wrote the following program:
int main()
{
string object="1";
std::stringstream objectString;
objectString << std::setprecision(8) << atof(object.c_str());
cout<<"ObjectString="<<objectString.str()<< " "<<std::setprecision(10) << double(atof(object.c_str())) <<"\n";
}
I expected the output to be:
ObjectString=1.0 1.0
However, I am getting the output as:
ObjectString=1 1
Can someone please suggest as to where am I going wrong?
To force trailing zeros, use std::fixed:
std::string object = "1";
std::stringstream objectString;
objectString << std::fixed << std::setprecision(8) << atof(object.c_str());
std::cout << "ObjectString=" << objectString.str() << " ";
std::cout << std::fixed << std::setprecision(10) << double(atof(object.c_str())) << "\n";
Output:
ObjectString=1.00000000 1.0000000000
The way to force the output to always show the decimal point is to use std::showpoint:
#include <iostream>
#include <iomanip>
int main() {
double d = 1.0;
std::cout << std::setprecision(1) << d << '\n';
std::cout << std::setprecision(1) << std::showpoint << d << '\n';
return 0;
}
[temp]$ ./a.out
1
1.
[temp]$
This question already has answers here:
Set back default floating point print precision in C++
(5 answers)
Closed 7 years ago.
I have search the web but couldn't find what I need.
Some people recommend using
streamsize ss = std::cout.precision();
but I couldn't get it to work.
How do I set a double value back to the original state after setprecision?
#include <iostream>
using namespace std;
int main()
{
double a;
cout << "enter a double value: ";
cin >> a;
cout << "your value in 3 decimals is " << setprecision(3) << fixed << a << endl;
cout << "your original value is " << a << endl;
return 0;
}
Obviously the code above will not return the original value of a.
My intended output is: if user enter 1.267432
your value in 3 decimals is 1.267
your original value is 1.267432
How do I set a double value back to the original state after
setprecision?
To do so, you have to get the precision before you use setprecision(). In your question you already mentioned it by the following line:
streamsize ss = std::cout.precision();
but I couldn't get it to work.
Here how you use it:
streamsize ss = std::cout.precision();
double a = 1.267432;
std::cout << "a = " << a << '\n';
std::cout.precision (3);
std::cout << "a becomes = " << a << '\n';
std::cout.precision (ss);
std::cout << "Original a= " << a << '\n';
And the output will be like:
a = 1.26743
a becomes = 1.27
Original a= 1.26743
Reference: setprecision.
Run live.
You can try like this:
#include <iomanip>
#include <iostream>
int main()
{
double a = 1.267432;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(3);
std::cout << a << endl;
return 0;
}