0

How to store a number 51588284.25 in C/C++?

I failed using float or double , as:

float x = 51588284.25;
cout << x; //5.15883e+07
double y = 51588284.25;
cout << y; //5.15883e+07

Maybe float cannot express this number?

phuclv
  • 37,963
  • 15
  • 156
  • 475
xxzhu
  • 11
  • 1
  • 2
    Use C `printf()` and format `"%.2f"` to format the value in a `double` with 2 decimal places. C++ is selecting a format that is inappropriate for you. You could adjust the format using manipulators, but you have to know which ones to use. Using `float` won't work reliably; it can't represent the number accurately. – Jonathan Leffler Mar 06 '20 at 04:04
  • 4
    It's storing properly (at least for `double`). The default formatting settings just don't show floating point numbers that large. Read up on the [float format manipulators](https://en.cppreference.com/w/cpp/io/manip/fixed). – ShadowRanger Mar 06 '20 at 04:05
  • 1
    Search your favorite C++ reference for "set_precision". – Thomas Matthews Mar 06 '20 at 04:13

0 Answers0