Hey guys,

I am making a calculator. It has two "screens", one showing input and one showing output. When the equals button is pressed on the calculator, the calculation is shown on the output screen.

I have come across this problem:

Let's say I type into the input screen "10" and then press equals, the output screen will show "10". Good, this is working correctly. It works correctly if I do the same but with "100", "1000", "10000" and "100000".

However, when I try it with "1000000" I get the following result:

"1e+006"

If I add any more numbers I get the same value but the 6 increases.


How do I change this so that it shows the raw number instead of this format?

For example, instead of showing "1e+006", I want it to show "1000000".


Some more information:

The reason the input works is because every time a button is pressed it adds it to the screen's string and then displays it. However, this method isn't practical for the output screen, so how can I do this another way?


Thanks in advance for any help that any of you provide.

Is is a console or gui program? How are you displaying the number?

If you are displaying floats then use fixed

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   float x = 1000000000000000.0F;
    cout << x << '\n';
    cout << fixed << x << '\n';
}

It's a GUI program. I'm using a streamer to convert the number to a string and back again then using a text label to display it on the calculator screen.

Here's some code:

InputOutputStream << CurrentCalculation; // The float carrying the calculated number
InputOutputStream >> TotalOutputString; // is converted to the string.
InputOutputStream.str("");
InputOutputStream.clear();
this->OutputLabel->Text = gcnew System::String(TotalOutputString.c_str()); // Displayed.

The quickest way is to put a setprecision into the stream:

InputOutputStream>>std::setprecision(12)>>TotalOutputString;

(You may need to add an #include <iomanip> to your file as that is the place for the definition. You don't need the std:: if you have the horrible, using namespace std; line).

That still means that you will get 1e13 if you enter a number over 100 billion.
However at those number you start seeing floating point rounding errors. e.g
you will start to see 999999995 (as floating point is roughly only accurate to 8 places.) If this is for financial calculations, do not use float or double.

What should I do if I want the number to stay accurate? What can I use instead of a float or double?

The normal approach is to use an integer. If you are working with decimal currency then you would use the number of pennies for examples, or even 1/1000 of a penny. Whatever accuracy you require. [Please replace "penny" by the correct currency unit].
To display the value, normally you would then divide by the correct amount, and then display. I might give you rounding errors in a display, but remains accurate so when you subtract two big numbers you still have an accurate small number left.

You may well need to use a long int, rather than int

I'll see what I can do, thanks to both of you!

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.