I have a template function based on istringstream and I've discovered that a string value of "99999.99" rounds up to 100000. In fact, I've found that the C++ iostreams in general are doing the rounding. Plain 'ole standard C printf() handles the job well. Is there any solution to this at all.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <unistd.h>
#include <algorithm>
using namespace std;
template <class T> bool from_string(T& t, const string& s, ios_base& (*f)(ios_base&))
{
istringstream iss(s);
return !(iss >> f >> t).fail();
}
class MyApp {
private :
public :
MyApp();
virtual ~MyApp();
void run();
operator bool() const { return true; }
};
MyApp::MyApp()
{
}
MyApp::~MyApp()
{
}
void MyApp::run()
{
string elementData = "99999.99";
double fproductUnitPrice = 0.0;
double myval = 99999.99;
double myval2 = 99999.95;
float myval3 = 99999.99;
from_string<double>(fproductUnitPrice, elementData, std::dec);
cout << "fproductUnitPrice=" << fproductUnitPrice << endl;
fproductUnitPrice -= 0.05;
cout << "fproductUnitPrice=" << fproductUnitPrice << endl;
cout << "myval=" << myval << endl;
cout << "myval2=" << myval2 << endl;
cout << "myval3=" << myval3 << endl;
printf("myval %lf\n", myval);
printf("myval2 %lf\n", myval2);
printf("myval3 %lf\n", myval3);
}
int main(int argc, char* argv[])
{
MyApp myApp;
myApp.run();
}
EDIT - The point about using float/doubles is understood, but no one has run the code I posted. The printf()s print the values correctly (with the exception of 9999.9999 which is a float). I'm more about why the discrepancy in the stdc library vs C++.
fproductUnitPrice=100000
fproductUnitPrice=99999.9
myval=100000
myval2=99999.9
myval3=10000
myval4=10000
myval 99999.990000
myval2 99999.950000
myval3 10000.000000
myval4 9999.999900