I think I have a convertingproblem when using stringstream.
I multiply 0.995 * 19.99 wich is: 19.89005

The first messageBox do show because 19.89 < 19.89005.

Then I convert Total to string and back to double again and do
the same check if: 19.89 < NewNumber
But here the MessageBox doesn´t show so I beleive there is something
that happens with the decimals when converting like I do. (Red Area)
Is it possible to not lose decimals when doing a conversion to string and back to
double like I do ?
Wonder if this is the problem that I think.

double Total = 0.995 * 19.99;  //Gives 19.89005
std::string Replacing;

if( 19.89 < Total )
{
      MessageBox::Show("Before");
}	
/*................................................*/

stringstream c1;
c1 << Total; 
Replacing = c1.str();

double NewNumber = 0;
stringstream v1(Replacing);
v1 >> NewNumber ;
if( 19.89 < NewNumber )
{
     MessageBox::Show("After");
}

Recommended Answers

All 4 Replies

change the precision of the stringstream

stringstream c1;
   c1.precision(100);
   c1 << Total; 
   Replacing = c1.str();

Change c1 << Total; to: c1 << std::setprecision(10) << Total; And don't forget to #include <iomanip> for this.

[edit]Darn, too slow

The trick is to use fixed c1 << fixed << setw(6) << Total;

That did solve the problem. setw(6) should then meen to put 6 characters as I red in MSDN.
Thank you...

The trick is to use fixed c1 << fixed << setw(6) << Total;

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.