bytes to megabyte conversion

I got some of this figured out but I need the result to read 49.9 how could I do that?

1
2
3
4
5
  double hdSpace = 49987742;
  hdSpace = hdSpace /1000/1000;

  //Output result = 49.9877
cout << setprecision (2) << fixed << hdSpace << endl;
Will display 49.99, anything else I tried rounded the number to 50.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>
#include <cmath>


int main()
{
    double hdSpace = 49987742;
    hdSpace = hdSpace /1000/1000;
    std::cout << std::fixed << std::setprecision(1);
    std::cout << std::trunc(hdSpace * 10)/10;
}


Yeah I noticed that std::setprecision(3) rounded me to 50 as well. I just don't want it to reduce by mb. 49,48,47 after reducing hdSpace. I'm really looking for 3 digits like 49.9,49.8,49.7

Sorry if the original question wasnt clear. I have a bad problem with that.

Thanks for the replys so far.

Miinipaa I haven't tried your code yet so I just woke up. Give me a second and I'll see if that's what I'm looking for.
Well I got an error. Trunc is not a member of std.
Well I guess it doesn't matter much so I'm going to go with sanualAdams ans wer and just use 49.99 output.

Thanks again for the replies.
You need C++11 for trunc. Compile with -std=c++11 or -std=c++0x Also just FYI: the previous rounding is working correctly because 50.0 is closer to 49.98 than 49.9.
Last edited on
Topic archived. No new replies allowed.