Hi i was wondering how to set a float to print out only two decimal places, when it is outputted to the screen.
The program involves money and you cant have 3 decimals with currency.
Example
32.4432
to
32.44
Thanks
Printable View
Hi i was wondering how to set a float to print out only two decimal places, when it is outputted to the screen.
The program involves money and you cant have 3 decimals with currency.
Example
32.4432
to
32.44
Thanks
Code:#include <iostream>
#include <iomanip>
int main()
{
double s = 2.3426;
std::cout << setprecision(2); //add this line
std::cout.setf(ios::showpoint | ios::fixed); // and this line after variables are declared
std::cout << s << std::endl; //output: 2.34
return 0;
}