This question already has answers here:
Printing the correct number of decimal points with cout
(13 answers)
Closed 6 years ago.
Similar topic is already discussed in the forum. But I have some different problem in following code:
double total;
cin >> total;
cout << fixed << setprecision(2) << total;
If I give input as 100.00 then program prints just 100 but not 100.00
How can I print 100.00?
cout << fixed << setprecision(2) << total;
setprecision specifies the minimum precision. So
cout << setprecision (2) << 1.2;
will print 1.2
fixed says that there will be a fixed number of decimal digits after the decimal point
cout << setprecision (2) << fixed << 1.2;
will print 1.20
It is possible to print a 15 decimal number in C++ using the following:
#include <iomanip>
#include <iostream>
cout << fixed << setprecision(15) << " The Real_Pi is: " << real_pi << endl;
cout << fixed << setprecision(15) << " My Result_Pi is: " << my_pi << endl;
cout << fixed << setprecision(15) << " Processing error is: " << Error_of_Computing << endl;
cout << fixed << setprecision(15) << " Processing time is: " << End_Time-Start_Time << endl;
_getch();
return 0;
The easiest way to do this, is using cstdio's printf. Actually, i'm surprised that anyone mentioned printf! anyway, you need to include the library, like this...
#include<cstdio>
int main() {
double total;
cin>>total;
printf("%.2f\n", total);
}
This will print the value of "total" (that's what %, and then ,total does) with 2 floating points (that's what .2f does). And the \n at the end, is just the end of line, and this works with UVa's judge online compiler options, that is:
g++ -lm -lcrypt -O2 -pipe -DONLINE_JUDGE filename.cpp
the code you are trying to run will not run with this compiler options...
Using header file stdio.h you can easily do it as usual like c. before using %.2lf(set a specific number after % specifier.) using printf().
It simply printf specific digits after decimal point.
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
double total=100;
printf("%.2lf",total);//this prints 100.00 like as C
}
This will be possible with setiosflags(ios::showpoint).
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;
I am trying to print a decimal no. in following format : "#####+3.01"
Case: There is a decimal no (let say 3.01 in this case). I have to print it with its sign +/- preceding with y no. of #, with some fix total width. (let say x = 10 in this case).
I tried do something like this :
double no = 3.01;
cout << setfill('#') << setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
But i am getting followinfg output :
+#####3.01
Expected Output :
#####+3.01
Your code gave me correct result. I am using a Linux machine.
Just in case it is a OS dependent problem, try this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double no = 3.01;
cout << setfill('#') << std::right<< setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
}
I want to globally set the ouptut precision to 2 decimal places.
I already tried to use iomanip and setprecision, however I keep getting output with 'e' in it.
This is my example code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double pay=16.78;
double hours;
double total;
cout.precision(2);
cout << "Please enter how many hours you worked : " << endl;
cin >> hours;
total=hours*pay;
cout << "You earned: " << total << endl;
}
You can use something like this:
double pay = 393.2993;
std::cout << std::fixed << std::setprecision(2) << pay;
You will need to include iomanip for this to work.
#include <iomanip>
I'm not familiar with "cout.precision(2)". I would use std::setprecision(4) if I wanted my output to have 4 significant figures.
If your e is a positive value, you cannot ride of them because your value is too large. This code
std::cout << std::setprecision(3) << 3e45;
//output
3e+45
If it's a negative number, your precision is not enough. Like the following code
std::cout << std::setprecision(3) << 3e-45; //
//output
3e-45
A simple way will be to use std::fixed
std::cout << std::fixed << std::setprecision(3) << 3.63e-2;
//output
0.036
But the disavantage you have with the std::fixed is it shows the zero after the last none-zero number until it reach the setprecision qunatity you set previously.
Is there any way in C++ (or boost lib) to show a given number digits of fraction part? But I don't want to print the trailing 0 in fraction part (eg. 1.000, 1.500). See this case:
cout << std::setprecision(3) << 5.0/7.0 << endl; // 0.714
cout << std::setprecision(3) << 12.0/7.0 << endl; // 1.71
cout << std::setprecision(3) << 7.0/7.0 << endl; // 1
cout << std::setprecision(3) << 10.5/7.0 << endl; // 1.5
The problem is setprecision prints line 1 and line 2 differently, where I want to have both lines print 0.714 and 1.714. And still keep line 3 and line 4 1 and 1.5.
How about something like:
#include <cmath>
using namespace std;
cout << setprecision(ceil(log10(floor(x))+3) << x;
Not exactly fast, but the idea is to figure out how many digits the integer part of x requires, then add the number of decimal places you're interested in to that. You could even write your own manipulator to do that if you were really serious about it.
I'd like to display numbers using a padding (if necessary) and a fixed number of digits. For instance, given the following numbers:
48.3
0.3485
5.2
Display them like this:
48.30
00.35
05.20
I'm trying combinations of std::fixed, std::fill, std::setw, and std::setprecision, but I can't seem to get what I'm looking for. Would love some guidance!
NOTE: The 0-padding isn't really critical, but I'd still like the numbers to be aligned such that the decimal point is in the same column.
It's pretty straightforward
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(2) << setfill('0');
cout << setw(5) << 48.3 << endl;
cout << setw(5) << 0.3485 << endl;
cout << setw(5) << 5.2 << endl;
}
Writing code like this makes me yearn for printf however.