 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Achim Domma (Procoders) Guest
|
Posted: Thu Feb 03, 2005 4:47 pm Post subject: string nach zahl mit locale / num_get |
|
|
Hallo,
ich würde gerne Strings, die Zahlen enthalten, sprachabhängig in 'echte'
Zahlen umwandeln. Die Sprache wird in der Software gesetzt, soll also
nicht vom OS abhängen. Konkret möchte ich zwischen deutsch und englisch
umschalten können, so daß einmal 123.45 und einmal 123,45 als Kommazahl
erkannt wird.
Basierend auf einem Beispiel von Dietmar Kühl, das ich via Google
gefunden habe, habe ich mir folgendes Beispiel zusammengebaut:
#include <locale>
#include <iostream>
#include <iterator>
#include <sstream>
int main(int argc, const char* argv[])
{
float value;
std::wstringstream source(L"123.45");
std::locale loc;
std::locale de("German_germany");
typedef std::istreambuf_iterator<wchar_t> InIt;
InIt from = InIt(source);
InIt end = InIt();
std::ios_base& fmt = source;
std::ios_base::iostate err;
std::num_get<wchar_t, InIt> const& ng
= std::use_facet<std::num_get(de);
ng.get(from, end, fmt, err, value);
if (err == std::ios_base::goodbit) {
std::cout << "value:" << value << std::endl;
} else {
std::cout << "not a float" << std::endl;
}
return 0;
}
Leider funktioniert es nicht. Erstmal wird nie der Wert ausgegeben, d.h.
err ist nie gleich goodbit. Außerdem ist es egal, ob ich loc oder de
verwende, 123,45 wird immer nur als 123 geparst. Kann mich jemand
erleuchten?
Gruß,
Achim
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Markus Schaaf Guest
|
Posted: Thu Feb 03, 2005 5:31 pm Post subject: Re: string nach zahl mit locale / num_get |
|
|
"Achim Domma (Procoders)" <domma (AT) procoders (DOT) net> schrieb:
| Quote: | ich würde gerne Strings, die Zahlen enthalten, sprachabhängig in 'echte'
Zahlen umwandeln. Die Sprache wird in der Software gesetzt, soll also
nicht vom OS abhängen. Konkret möchte ich zwischen deutsch und englisch
umschalten können, so daß einmal 123.45 und einmal 123,45 als Kommazahl
erkannt wird.
Basierend auf einem Beispiel von Dietmar Kühl, das ich via Google
gefunden habe, habe ich mir folgendes Beispiel zusammengebaut:
#include <locale
#include
#include
#include
int main(int argc, const char* argv[])
{
float value;
std::wstringstream source(L"123.45");
std::locale loc;
std::locale de("German_germany");
typedef std::istreambuf_iterator
InIt from = InIt(source);
InIt end = InIt();
std::ios_base& fmt = source;
std::ios_base::iostate err;
std::num_get<wchar_t, InIt> const& ng
= std::use_facet<std::num_get(de);
ng.get(from, end, fmt, err, value);
if (err == std::ios_base::goodbit) {
std::cout << "value:" << value << std::endl;
} else {
std::cout << "not a float" << std::endl;
}
return 0;
}
Leider funktioniert es nicht.
|
Vielleicht hilft Dir das als Ausgangspunkt:
#include
#include <iostream>
#include <string>
#include <sstream>
void test( char const* l, std::wstring const& s )
{
std::locale loc(l);
std::wstringstream source( s );
double value;
std::wcout << l << ": " << ' ' << s << " -> ";
source.imbue( loc );
if( source >> value ) std::wcout << value << 'n';
else std::wcout << "failedn";
}
int main()
{
test( "German", L"123.45" );
test( "German", L"123,45" );
test( "C", L"123.45" );
test( "C", L"123,45" );
return 0;
}
Ausgabe:
German: 123.45 -> failed
German: 123,45 -> 123.45
C: 123.45 -> 123.45
C: 123,45 -> 123
Falls die erste Zeile unklar ist: Bei mir sind '.' Tausender-Trennzeichen.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Achim Domma (Procoders) Guest
|
Posted: Thu Feb 03, 2005 7:49 pm Post subject: Re: string nach zahl mit locale / num_get |
|
|
Markus Schaaf wrote:
| Quote: | Vielleicht hilft Dir das als Ausgangspunkt:
|
Danke, sieht aus, alsob es genau das wäre, was ich suche. Ich versteh's
zwar ncht 100% aber das kommt sicher noch.
| Quote: | German: 123.45 -> failed
German: 123,45 -> 123.45
C: 123.45 -> 123.45
C: 123,45 -> 123
Falls die erste Zeile unklar ist: Bei mir sind '.' Tausender-Trennzeichen.
|
Was heißt bei dir? Macht deine Standardlib das so? Hast du das irgendwo
eingestellt? D.h. 123.456 wird bei dir als 123456 geparst? Könnte ich
was tun, damit 'C' sich bei 123,45 genauso verhält?
Gruß,
Achim
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Markus Schaaf Guest
|
Posted: Thu Feb 03, 2005 10:01 pm Post subject: Re: string nach zahl mit locale / num_get |
|
|
"Achim Domma (Procoders)" <domma (AT) procoders (DOT) net> schrieb:
| Quote: | Was heißt bei dir? Macht deine Standardlib das so? Hast du das irgendwo
eingestellt?
|
Ich kann das einstellen, ja.
| Quote: | D.h. 123.456 wird bei dir als 123456 geparst? Könnte ich
was tun, damit 'C' sich bei 123,45 genauso verhält?
|
Ich glaube, wie "C" sich zu verhalten hat, ist festgelegt. Ich verstehe
auch nicht, was Du willst. Wenn Du ein bestimmtes Verhalten brauchst,
mußt Du das selber programmieren. Locales sind dafür gedacht, das zu
machen, was der Benutzer sich wünscht, nicht der Programmierer. Du kannst
natürlich das gleiche Interface benutzen, das in den Streams breits
integriert ist. Vielleicht reicht schon eine eigene »num_punct« Facette.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|