Scritto da
Non Autenticato
il 2009-06-24 13:51:00.0 alle 2009-06-24 13:51:00.0
ovvero leggere i numeri con la virgola...
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
using namespace std;
class MyPunct: public numpunct_byname<char> {
protected:
char do_decimal_point() const { return ','; }
/*
* the locale class manages refcounting and
* destruction of the facets
*/
~MyPunct() {}
public:
explicit MyPunct(const char* locname)
: numpunct_byname<char>(locname) {}
};
int main()
{
locale loc(locale("C"), new MyPunct("C"));
istringstream in("426,21");
in.imbue(loc);
double d = 0.0;
in >> d;
if (in.fail() || !in.eof()) {
cerr << "YOU'VE FAILED PLEASE DIE" << endl;
return -1;
}
cout << fixed << setprecision(2) << d << endl;
return 0;
}
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
using namespace std;
class MyPunct: public numpunct_byname<char> {
protected:
char do_decimal_point() const { return ','; }
/*
* the locale class manages refcounting and
* destruction of the facets
*/
~MyPunct() {}
public:
explicit MyPunct(const char* locname)
: numpunct_byname<char>(locname) {}
};
int main()
{
locale loc(locale("C"), new MyPunct("C"));
istringstream in("426,21");
in.imbue(loc);
double d = 0.0;
in >> d;
if (in.fail() || !in.eof()) {
cerr << "YOU'VE FAILED PLEASE DIE" << endl;
return -1;
}
cout << fixed << setprecision(2) << d << endl;
return 0;
}
-
locale, facets, c++, fanchiù @ 2009-06-24 13:51:00.0 di Non Autenticato