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

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;

Related

Reading integer [duplicate]

This question already has answers here:
How can I pad an int with leading zeros when using cout << operator? [duplicate]
(7 answers)
Closed 9 years ago.
Here's the code I'm trying to change
string binary = "000000100001000100010000000100000"
bitset<32> set(binary);
cout << hex << set.to_ulong() << endl;
The code shows 2112010 but I want it to show 02112010.
std::cout << std::setfill('0') << std::setw(5) << i << std::endl;
that is the same number you can format it with the 0 by using format specifiers if you need to retain the zero you need to store it as a string,

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;

Rounding to 2 decimal points [duplicate]

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;

C++ Floating point Display [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
floating point precision
when I do cout<<8.0 .Its getting printed as 8.How to Print in the output console of c++ the entire zeros after decimal point like 8.00000000
I tried this cout<<setprecision(5)<<(double)8.0; still printing 8
Use the fixed manipulator
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(6) << (double)8 << "\n";
return 0;
}
http://ideone.com/ShcNIc
See How do I print a double value with full precision using cout?
cout.precision(15);
cout << fixed << 8.0;

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;
}