locale independent atof?

S

sprotty

I'm writting some classes that deal with XML, and they need to be able
to convert from strings to doubles and back.

The problem is they should only accept/output strings valid in XML,
that is using the '.' as the decimal separator (ie '123.12').

Ive been using atof etc, but as soon as you install on a foregin
machine, you start getting numbers that look like '12,343,243' or
'32432,98' etc. These are not valid in XML documents, and thus cause
problems.

Are there any locale independant functions or source code for
convertion functions knocking around (it need to be pretty platform
independant as well).
Any help would be appresiated.

Thanks Simon Sprott
 
D

Dietmar Kuehl

Ive been using atof etc, but as soon as you install on a foregin
machine, you start getting numbers that look like '12,343,243' or
'32432,98' etc. These are not valid in XML documents, and thus cause
problems.

Can you please point out where the XML specification prohibits the
use of '12,343,243'? It is some time since I looked at the XML
specification but last time I looked it did not address floating
point values at all and in all contexts where I would expect
floating point values (attribute values and text) the mentioned
strings are clearly valid. However, this issue is somewhat
irrelevant to the discussion of locale independent formatting.
Are there any locale independant functions or source code for
convertion functions knocking around (it need to be pretty platform
independant as well).

You can simple use the numeric formatting functions of the "C"
locale, either directly or imbued into an appropriate stream, e.g.:

| std::eek:stringstream out;
| out.imbue(std::locale("C"));
| out << 12343243.0;

This will not introduce any thousand separators and will use a
point ('.') as decimal point.
 
W

wittempj

I'm writting some classes that deal with XML, and they need to be able
to convert from strings to doubles and back.

The problem is they should only accept/output strings valid in XML,
that is using the '.' as the decimal separator (ie '123.12').

Ive been using atof etc, but as soon as you install on a foregin
machine, you start getting numbers that look like '12,343,243' or
'32432,98' etc. These are not valid in XML documents, and thus cause
problems.

Are there any locale independant functions or source code for
convertion functions knocking around (it need to be pretty platform
independant as well).
Any help would be appresiated.

Thanks Simon Sprott

You can stream to a double like this:
#include <cstdlib>
#include <sstream>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
stringstream s;
double f;

s << 3.4;

s >> f;

if (s.peek() == EOF)
{
cout << " Ok" << endl;
}

return EXIT_SUCCESS;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top