locale independent atof?



 DEVELOP > c-Plus-Plus > locale independent atof?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 17 Jan 2005 09:06:06 AM
Object: locale independent atof?
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
.

User: ""

Title: Re: locale independent atof? 17 Jan 2005 09:37:28 AM
wrote:

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;
}
.

User: "Dietmar Kuehl"

Title: Re: locale independent atof? 17 Jan 2005 09:16:07 AM
wrote:

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::ostringstream out;
| out.imbue(std::locale("C"));
| out << 12343243.0;
This will not introduce any thousand separators and will use a
point ('.') as decimal point.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER