Set Precision

Hi all,

How to set the precision to 2 digits for floating point numbers in cpp?
for ex: float x = 45.223455666777;
I want to set to 45.22

How to do it with the precision?

Thank you,
I am using the same but still I am not able to get the precision.

1
2
 float x = 45.878745645464;
        std::cout << std::setprecision(2) << x << '\n';


The above code will just print 45. It is not at all printing the numbers after the decimal point.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

int main()
{
    float x = 45.878745645464;
    std::cout << std::fixed << std::setprecision(2) << x << '\n';
    
    return 0;
}



45.88
Program ended with exit code: 0
Another permutation of the same just for luck:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip> // <--

int main()
{
    float x = 45.878745645464;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << x << '\n';
    
    x *= 1000;
    std::cout << x << '\n';
    
    return 0;
}



45.88
45878.75
Program ended with exit code: 0
1
2
3
4
5
6
7
8
int main()
{
    float x = 45.878745645464;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << x << '\n';
    
    return x;
}


In the above code I have to return the x value to other function. By doing so, The value x is not updated.
It still says x = 45.878745645464. How to return the value after using setprecision?
You don't. fixed/setprecision are for stream insertion only (<<). You use them when you want to display the numbers or output to a file stream etc.

Due to how floating point numbers are stored internally, you can't insist upon a specific value (the same way you can't insist upon a specific value for say 1 / 3).

There are ways to do this such as (int)(x * 100) / 100.0 which multiples the number by 100, casts to an int to remove any more decimals and then divides by 100 to get the 2DP - but usually this again would be something done for output.

Why do you need the returned value to be 2DP ?
Last edited on
One rough-and-ready way is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip> // <--

int main()
{
    float x = 45.878745645464;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << x << '\n';
    
    x *= 1000;
    std::cout << x << '\n';
    
    float y = 45.878745645464;
    float y_modified = (int)(y * 100)/100.;
    std::cout << y_modified << '\n';
    
    
    return 0;
}


Otherwise use more sophisticated rounding functionality floor,ceil, round etc in <cmath>
It still says x = 45.878745645464. How to return the value after using setprecision?
Why do you want to lose the precision?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;


void newround( float &x, int factor, int &front, int &back )
{
   long long temp = (long long)( factor * x + 0.5 - ( x < 0 ) );
   front = temp / factor;
   back = abs( temp ) % factor;
   x = (float)temp / factor;            // This is NOT exact due to finite precision
}


int main()
{
   float x = 45.878745645464;          // Excessive precision for a float
   cout << fixed << setprecision(2) << x << '\n';
   
   printf( "%.2f\n", x );
   
   int front, back;
   newround( x, 100, front, back );
   cout << front << "." << back << '\n';
   cout << x << '\n';
}


You could also insert (with the relevant precision) into a stringstream, then take the resulting string and convert it to a float.
Last edited on
Thank you all for the reply.
Topic archived. No new replies allowed.