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.
Related
This example program was created with the sole purpose of showing what setprecision and setw does. I dont understand the purpose of the third line that says "setprecision(5)". I commented the line out to see the difference but it looks the exact same. Is there no purpose?
cout << "\nSales Figures\n";
cout << "-------------\n";
cout << setprecision(5);
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;
The setprecision() function is part of the "iomanip" library and is used when you need to output a float to a certain number of decimal places. This is good for displaying money amounts and other things that typically are shown with a set number of digits after the decimal point (even if those digits are 0).
Say you have a float called price: If you stored 10.0 in that float, C++ would not know how many decimal points to output when you print into the screen; setprecision(2) would make the output 10.00.
You can find the documentation at this link: https://cplusplus.com/reference/iomanip/setprecision/.
It includes the following code as an example of how setprecision() works.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.1415
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.14159
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.14159
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.141590000
return 0;
}
Note that setprecision() is only applicable to data types with decimal points such as floats and doubles.
I want to display the values (in hex) of these certain registers and counters but I want to limit the number of digits being displayed.
cout << "Acc register : " << hex << Acc << ","; //display 2 digits
cout << " X register : " << hex << X << ","; //display 3 digits
cout << " Program counter : " << hex << PC << ","; //display 3 digits
I also want to display preceding zeros if the value was only 1 digit long, for example if
program counter = 4
PC should display as Program counter : 004
I have searched the internet to try and find a solution but I can not seem to find something that works. Can anybody explain how to do this please. Many thanks.
The STL comes with a header called iomanip, which can modify in the way you want and even more.
A short example would be:
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::setfill ('x');
std::cout << "PC" << std::setw(3) << 4 << std::endl;
}
An additional example can be found here:
http://www.cplusplus.com/reference/iomanip/setfill/
An overview over all options here:
http://www.cplusplus.com/reference/iomanip/
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.
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).
I'm learning the setw and setprecision functions, so here is what I've been trying so far and I have a few questions.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float y = 1.25;
cout << fixed << setw(10) << setprecision(2) << y << endl;
cout << "\n\n\nx\n";
float x = 213565544.4826;
cout << fixed << setw(13) << setprecision(3) << x << endl;
cout << fixed << setw(14) << setprecision(3) << x << endl;
cout << fixed << setw(15) << setprecision(3) << x << endl;
cout << fixed << setprecision(3) << x;
cout << "\n\n\nz\n";
float z = 213565544.4826;
cout << setw(11) << setprecision(1) << x << endl;
cout << fixed << setw(12) << setprecision(1) << x << endl;
cout << fixed << setw(11) << setprecision(1) << x << endl;
cout << setw(12) << setprecision(1) << x << endl;
cout << "\n\n\nm\n";
float m = -344.275;
cout << fixed << setprecision(1) << x << endl;
cout << fixed << setw(8) << setprecision(1) << x << endl;
cout << fixed << setw(7) << setprecision(1) << x << endl;
cout << fixed << setw(6) << setprecision(1) << x << endl;
return 0;
}
And the input is :
1.25
x
213565552.000
213565552.000
213565552.000
213565552.000
z
213565552.0
213565552.0
213565552.0
213565552.0
m
213565552.0
213565552.0
213565552.0
213565552.0
So, now my questions are :
1) Why do we use "fixed" in first place?
If we look at this example:
cout << setw(11) << setprecision(1) << x << endl;
cout << fixed << setw(11) << setprecision(1) << x << endl;
They output the same value, so what does fixed really change?
2) How does setw work for negative numbers?
In the last example of m. The result is the same for all examples, what does the - sign change in the setw ?
213565552.0
213565552.0
213565552.0
213565552.0
Where do these numbers come from? The value of m is totally different from the ones outputted.
3) Does the . in the number counts as 1 place?
For example, we have number 1.23 and setw(10)
There would be 6 spaces before and then 1.23 (because the dot is counted as 1). Is that true?
4) Why does setprecision is used along with setw? Why does 0000s appear if it's not used? Does it appear as many 0s as the float can handle?
5) Why is the value for x
213565552.000
213565552.000
213565552.000
213565552.000
If x = 213565544.4826.
Where does the numbers 44.4826 get lost?
These seem to be 5 questions rather than one. Anyway:
std::fixed is used to indicate that you always want to have a fixed point format rather than using scientific notation where this notation is more appropriate. When there are many digits needed to represent the value reasonably, the format will switch use x.yyyyyyEee (you can ask to always use scientific format using std::scientific).
std::setw() doesn't care what value is formatted! When a value is formatted and there is a positive out.width() set, the output will be padded with out.fill() character to be at least out.width() characters wide. If the output is bigger than out.width() anyway, no padding will occur. After each output operation [which takes out.width() into account] the out.width() is reset to 0 (all other formatting options are not automatically reset).
Any character counts towards the width, including the sign, thousands separators, decimal points, etc. The decimal point does not count towards the precision: out.precision() is the number of fractional digits (for std::fixed formatting) or the number of non-exponent digits (for std::scientific formatting).
The width is how many characters will be filled by the output, the precision specifies how many [fractional] digits are to be produced.
Binary floating point values can represent very few decimal digits (for float it is normally 6; you can find out how many digits can be safely used by using std::numeric_limits<float>::digits10). Trying to use any more digits than that will probably result in unexpected output when processing decimal values (when processing binary values you may be interested in up to std:numeric_limits<float>::digits places). You might want to have a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.