Rounding to 2 decimal points [duplicate] - c++

This question already has answers here:
Round double to 3 points decimal [duplicate]
(4 answers)
Closed 4 years ago.
I've used the following to round my values to 2 decimal points:
x = floor(num*100+0.5)/100;
and this seems to work fine; except for values like "16.60", which is "16.6".
I want to output this value like "16.60".
The way I'm outputting values is the following:
cout setw(12) << round(payment);
I've tried the following:
cout setw(12) << setprecision(2) << round(payment);
But that gave me answers like
1.2e+02
How can I output the values correctly?

This is because std::setprecision doesn't set the digits after the decimal point but the significant digits if you don't change the floating point format to use a fixed number of digits after the decimal point. To change the format, you have to put std::fixed into your output stream:
double a = 16.6;
std::cout << std::fixed << std::setprecision(2) << a << std::endl;

you can use printf / sprintf or other similar functions. Following code will format floating point value into two digits after decimals. Refer to the printf manual for more formatting info
float f = 1.234567
printf("%.2f\n", f);

From Trevor Boyd Smith's comment:
If you are allergic to printf and friends there is the type safe C++ version in #include <boost/format.hpp> which you can use to do:
float f = 1.234567;
cout << boost::format("%.2f") % f << endl;

Related

How to display multiple leading zeros for floating point values in C++? [duplicate]

This question already has answers here:
How can I pad an int with leading zeros when using cout << operator? [duplicate]
(7 answers)
Closed 7 years ago.
In a C++ program, I want to display a column of floating point values so that the sign, digits, and decimal point all line up. Multiple leading zeros should pad the whole number part of each value, when necessary. For example:
A column of floating point values:
+000.0012
-000.0123
+000.1235
-001.2346
+012.3457
-123.4568
I had an elaborately commented test program that demonstrated the problem. But, as I was editing this post, I found the answer I need here:
- Extra leading zeros when printing float using printf?
The essential problem was that I was using a format code of "%+04.4f" when I should use "%+09.4f", because the total field width I want is 9:
1 for the sign
3 for the whole digits
1 for the decimal point
4 for the fractional digits
I do not have enough reputation points to comment on that post, so thank you from here, #AndiDog.
I still do not know how to get multiple leading zeros using just stream formatting flags. But that is a battle for another day. I will stick with a mixture of printf and stream for now.
A couple of comments have mentioned std::setfill('0') and std::setw. While these are necessary, they're not sufficient to the task. For example, this code:
std::cout << std::setfill('0') << std::setw(7) << std::showpos << 0.012;
will produce: 0+0.012 as its output. This is obviously not quite what we wanted.
We need to add the std::internal flag to tell the stream to insert "internal padding" -- i.e., the padding should be inserted between the sign and the rest of the number, so code like this:
std::cout << std::setfill('0') << std::setw(7) << std::internal << std::showpos << 0.012;
...produces the output we want: +00.012.
Also note that the padding character is "sticky", so if you alternate between using std::setw with numeric and non-numeric types, you'll probably need/want to change it each time. Otherwise, something like std::cout << setw(12) << name; will produce results like: 0000000Jerry, which is rarely desired either.
To assure that we always get the same number of places after the decimal point, we also need to set the std::fixed flag, and specify the number of places with std::setprecision, such as:
#include <iostream>
#include <iomanip>
#include <vector>
int main() {
std::vector<double> values { 0.1234, 1.234, 1.5555 };
for (auto d : values)
std::cout << std::internal << std::showpos << std::setw(9)
<< std::setprecision(3) << std::setfill('0') << d << "\n";
}
Which produces the output I believe is desired:
+0000.123
+0001.234
+0001.556
There is one circumstance under which you won't get aligned results this way though: if you have a number too large to fit into the field provided, all the places before the decimal point will still be printed. For example, if we added 1e10 to the list of numbers to be printed by the preceding code, it would be printed out as: +10000000000.000, which obviously won't align with the rest.
The obvious way to deal with that would be to just put up with it, and if it arises often enough to care about, increase the field size to accommodate the larger numbers.
Another possibility would be to use fixed notation only the number is below a certain threshold, and switch to (for example) scientific notation for larger numbers.
At least in my experience, code like this tends to be used primarily for financial data, in which case the latter option usually isn't acceptable though.
To show the positive sign, you use std::showpos.
To show the leading zeros, you use std::setw(n) and std::setfill('0').
To show the digits after zero, you use std::setprecision(m).
To show the zeros between the + sign and the first digit, you use std::internal.
To keep the digit at a fixed position, you use std::fixed.
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =1.234;
double g =-12.234;
std::cout << std::showpos<< std::internal << std::fixed << std::setprecision(4)<<std::setw(9) <<std::setfill('0') << f << '\n';
std::cout <<std::setw(9)<< std::setfill('0') <<g<<"\n"; //repeat these for a new number
return 0;
}
//output:
//+001.2340
//-012.2340
The only way I now how to do this is to display the sign first and then set the fill, width and precision after and display the positive value as you have already displayed the sign. You also need to set the format flag to ios::fixed
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float x[] = { 000.0012, .0123, .1235, 1.2346, 12.3457, 123.4568 };
cout.setf(ios::fixed);
for (int i = 0; i < 6; i++)
cout << (x[i] > 0 ? '+' : '-') << setfill('0') << setw(8) << setprecision(4) << abs(x[i]) << endl;
return 0;
}
Displays
+000.0012
-000.0123
+000.1235
-001.2346
+012.3457
-123.4568

Printing floating-point GPA in C++ [duplicate]

This question already has answers here:
Floating point format for std::ostream
(8 answers)
Closed 8 years ago.
my problem is when I am trying to print a floating-point GPA in C++.
It seems like a simple issue, but I can't get it to work. Basically I have a floating point value set to 4.0 for a GPA. However, when I try to print it like this:
cout << gpa << endl;
I get the value of 4. Without the .0 on the end. However, I want the .0 to show up. I have tried setting a precision but with no luck. Any help is appreciated.
You can use std::fixed in conjunction with std::setprecision
#include <iostream> // std::fixed
#include <iomanip> // std::setprecision
int main() {
double gpa = 4.0;
std::cout << std::fixed << std::setprecision(1) << gpa << std::endl;
return 0;
}
// Output is 4.0
#include <iomanip>
...
cout.setf(ios::fixed); // use fixed-point notation
cout.setf(ios::showpoint); // show decimal point
cout.precision(1);
...
cout << gpa << endl;

Truncate float so as to have only two decimals [duplicate]

This question already has answers here:
Set the digits after decimal point
(7 answers)
Closed 9 years ago.
C++
I would like to cout float f = 2.3333, but only with two decimals. How do I do that?
I remember something like this, but it doesn't work:
cout << f:2 << endl;
Using stream manipulators fixed and setprecision:
#include <iomanip>
float f = 2.3333;
std::cout << std::setprecision(2) << std::fixed << f;
I managed to solve it without iomanip:
cout << (((int)f*100) % 100)/100;

Display a number decimal format instead as an exponential in cout

I calculated a total of floats and I got a number like 509990e-405. I'm assuming this is the short version; how can I cout this as a full number?
cout << NASATotal << endl;
is what I have now.
You can force the output to be not in scientific notation, and to have the sufficient precision to show your small number.
#include <iomanip>
// ...
long double d = 509990e-405L;
std::cout << std::fixed << std::setprecision(410) << d << std::endl;
Output:
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050999000000
If you really want this is another question.
You can write your own BigNumber class that stores the results as strings. You would have to implement all of your numeric operations and I'm guessing performance will be an issue. But it can be done, no problem -- assuming that is what you want.

How do I display more decimals in the output console? [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
I want to output the value of a double in it's full precision. However, when using the cout function, it only displays the first 6 digits even though there is around 15-16 digits of precision.
How do I get my program to display the entire value, including the magnitude (power) component?
Use the setprecision() manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
You can also force scientific notation with the scientific manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/scientific/
cout << scientific << setprecision(15) << my_number << endl;
you could use something like this :
#include <iomanip>
cout << setprecision (9) << double_value << endl;
more iomanipulators, here
You're looking for setprecision (code taken from link):
int main () {
double f =3.14159;
cout << setprecision(15) << f << endl;
return 0;
}