dorkwad -10 Light Poster

I'm attempting to output a 7 digit number in a space 7 columns wide with cout, where loan is my 7 digit number:

cout << "$" << setw(7) << fixed << showpoint << setprecision(0) << loan;

Any attempts fiddling with this result in the number being converted to 1e+006. I have no problems with smaller numbers in the same place. The loan variable is a double. The columns are set up because the output has to line up with a table.

Any help or further inquiry would be much appreciated.

Dani AI

Generated

As reported, the C++ stream library can choose scientific notation for floating-point output when the stream is in the library's "general" mode and the value's exponent is outside the range allowed by the current precision. That switching behavior is part of the standard "defaultfloat" rules (the stream will pick fixed or scientific depending on exponent vs. precision) — see .

Practical fixes:

  • If the value is truly an integer amount (no cents), print it as an integer type to avoid floating formatting and loss of alignment. For example, cast to an integer type before streaming:

    cout << "$" << setw(7) << static_cast<long long>(loan);

    Take care: casting truncates the fractional part and requires an integer type wide enough for the value.

  • If the value must remain a floating point, force fixed notation or increase the stream precision so the formatter does not choose scientific. The meaning of precision differs by mode, so read the setprecision rules carefully: setprecision.

Quick troubleshooting: print the stream floatfield flags to confirm whether fixed/scientific/default is set, and check the active precision. Adjusting those (or switching to integer output) will stop a 7-digit number from being shown as 1e+006.

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.