Click here to Skip to main content
15,886,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all i have issue with _tstof when we change Decimal in Region and language from . to ,

Basically i have CString of data i have to seprate the comma and i have to take double value form the string.

it working when the Decimal in Region and language is in . but its nt wrking when it changes to ,

Could you please help me regarding the same.

What I have tried:

nData= (1 + str.Find(_T(","), nData+ 1 - 1));
_tstof(CString(str).Mid(nData+ 1 - 1)))
Posted
Updated 17-Oct-17 3:44am

The C library uses it's own localisation settings and is always initially the generic "C" setting. To apply the same settings as for the current Windows user, call
C++
setlocale(LC_ALL, "");
at program start (e.g. in InitInstance for Windows applications or on top of main for command line applications; requires inluding of locale.h).

To set the locale for the C++ library, include locale (without .h) and call
C++
std::locale::global(std::locale(""));
That will also set the locale for the C library.

Note also that the CString class uses the C library to format strings. So the decimal point set by the above calls is also used when passing floating point numbers to CString::[Append]Format().
 
Share this answer
 
Comments
Member 12677926 17-Oct-17 10:17am    
setting to locale cause any problem ?
Jochen Arndt 17-Oct-17 10:27am    
No, it is common to do that.

But you must know what it does. If you need for example to read and parse data from a file that uses the dot as decimal point you have to change the locale again for this task (or write a function that replaces the deciaml point character).
Member 12677926 17-Oct-17 10:35am    
::_tsetlocale(LC_NUMERIC, _T("C")); i had kept it is wrking
Jochen Arndt 17-Oct-17 11:19am    
That will select the dot as decimal point and is the default at program start.

When using atof (and strtod) you just have to ensure that the locale matches the decimal piunt used in the string.
Member 12677926 17-Oct-17 10:35am    
is it ok ?
Converting decimals is more tricky than a lot of people think, because there are local settings. Sometimes the "." and sometimes the "," stands for the decimal delimiter. The software must respect that setting. Normally is the setting used, when the program starts.

Read the documentation of _tstof to understand the behavior better. At the end is some sample code which demonstrates the explicit use of some locale settings.

Think about some input validation, for instance changing the "," to "." in the inputted string, or complaining a message that the data format is invalid. This is typical "UI behind" code...
 
Share this answer
 
Comments
Member 12677926 17-Oct-17 10:18am    
there is any solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900