[try Beta version]
Not logged in

 
Displaying floats properly.

Feb 15, 2018 at 10:35am
I'm having issues displaying the full number of a float.

Example:
#include <iostream>
using namespace std;

float apple = 1.50f;
int main()
{
cout << float(apple) << endl;
// Outputs: '1.5', instead of: '1.50'.
return 0;
}

Is there a way I can solve this issue ?
Last edited on Feb 15, 2018 at 10:38am
Feb 15, 2018 at 10:39am
If you always want to show two digits after the decimal dot you can include <iomanip> and use std::fixed and std::setprecision(2).

1
2
3
4
std::cout << std::fixed << std::setprecision(2);

float apple = 1.5f;
std::cout << apple; // outputs 1.50 
Feb 17, 2018 at 3:47am
It works!
Thanks, heaps bro.
Topic archived. No new replies allowed.