473,465 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Jul 22 '05 #1
2 5233
spro...@hotmail.com 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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2

spro...@hotmail.com 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;
}

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Epler | last post by:
I'd expect these two to be identical, but they don't seem to be. >>> import locale >>> locale.setlocale(locale.LC_ALL, "fr_FR") 'fr_FR' >>> locale.nl_langinfo(locale.DAY_1) 'dimanche' >>> #...
3
by: Christian Robottom Reis | last post by:
The LC_NUMERIC PEP has just been allocated a number, PEP 331. http://www.python.org/peps/pep-0331.html Though the code has been integrated, comments and corrections to the PEP would be...
2
by: Teis Draiby | last post by:
Does this already exist? Something like "\£" that would appear like a "," or "." dependent on the current locale settings. Examples: string stringValue = "1000\£54"...
3
by: Jakob Bieling | last post by:
Hi, Did not really know a short subject line, which describes my question better. I want to figure out, if a specific character is an alphabetic character, without having to be locale specific....
6
by: Sreekanth | last post by:
Hello, Am trying to convert a string to float. Am using atof() for that purpose. But the return value for atof is same for the string "0.0" and for some invalid input "Invalid". Can any body...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
5
by: Holger Klawitter | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi there, triggered by the recent questions about sorting, I started digging into my problems with upper('ä')='ä' when using LC_CTYPE and LANG =...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
24
by: Donn Ingle | last post by:
Hello, I hope someone can illuminate this situation for me. Here's the nutshell: 1. On start I call locale.setlocale(locale.LC_ALL,''), the getlocale. 2. If this returns "C" or anything...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.