Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > locale independent atof?

Reply
Thread Tools

locale independent atof?

 
 
sprotty@hotmail.com
Guest
Posts: n/a
 
      01-17-2005
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

 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-17-2005
(E-Mail Removed) 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: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.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      01-17-2005

(E-Mail Removed) 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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create C++ std::locale without changing C locale dertopper@web.de C++ 4 08-26-2008 01:15 PM
Is unicode.lower() locale-independent? Robert Kern Python 14 01-13-2008 02:11 AM
i18n problem, involving Locale.getDisplayLanguage and Locale.getDisplayCountry Maurice Hulsman Java 1 07-25-2004 06:11 PM
PEP 331: Locale-Independent Float/String Conversions Christian Robottom Reis Python 3 07-20-2004 04:47 PM
locale.nl_langinfo(RADIXCHAR) vs locale.localeconv()['decimal_point'] Jeff Epler Python 2 08-31-2003 02:18 PM



Advertisments