locale independent atof?

Discussion in 'C++' started by sprotty@hotmail.com, Jan 17, 2005.

  1. Guest

    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
    , Jan 17, 2005
    #1
    1. Advertising

  2. 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::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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
    Dietmar Kuehl, Jan 17, 2005
    #2
    1. Advertising

  3. Guest

    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;
    }
    , Jan 17, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,817
    Guus Bosman
    Jul 25, 2004
  2. Christian Robottom Reis

    PEP 331: Locale-Independent Float/String Conversions

    Christian Robottom Reis, Jul 19, 2004, in forum: Python
    Replies:
    3
    Views:
    550
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Jul 20, 2004
  3. Robert Kern

    Is unicode.lower() locale-independent?

    Robert Kern, Jan 12, 2008, in forum: Python
    Replies:
    14
    Views:
    964
    Robert Kern
    Jan 13, 2008
  4. Replies:
    4
    Views:
    980
  5. Gabriel Genellina
    Replies:
    0
    Views:
    679
    Gabriel Genellina
    Feb 18, 2009
Loading...

Share This Page