Hi all,

I just want to ask about setprecision because I'm a bit confused.

here's the code:

#include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    
    {
      double rate = x;
      cout << fixed << setprecision(2) << rate;
    }

where x = to following:

the left side of equation are the values of x.

1.105 = 1.10 should be 1.11

1.115 = 1.11 should be 1.12

1.125 = 1.12 should be 1.13

1.135 = 1.14 which is correct

1.145 = 1.15 also correct


but if x is:

2.115 = 2.12 which is correct

2.125 = 2.12 should be 2.13


so why in a certain value it's correct but sometimes it's wrong?

please enlighten me. thanks

BUMP

commented: Do not bump your threads. It's unnecessary and rude. We'll get to it when someone has something to say. -3

Read this, particularly the section on Representable numbers, conversion and rounding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.