_Tstof问题,区域和语言中的小数变化 [英] _Tstof issue with decimal change in region and language

查看:133
本文介绍了_Tstof问题,区域和语言中的小数变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们改变Region和语言中的Decimal时,我对_tstof有疑问。 to,



基本上我有数据的CString我必须分隔逗号,我必须从字符串中取双倍值。



当区域和语言中的十进制位于时,它工作。但是当它改变的时候它就会变成,



你可以帮我解决一下。



我尝试了什么:



nData =(1 + str.Find(_T(,),nData + 1 - 1)) ;

_tstof(CString(str).Mid(nData + 1 - 1)))

解决方案

C库使用它自己的本地化设置并且最初始终是通用的C设置。要应用与当前Windows用户相同的设置,请调用

 setlocale(LC_ALL, ); 

(例如,对于Windows应用程序的 InitInstance main 用于命令行应用程序;需要包含 locale.h )。



To设置C ++库的语言环境,包括 locale (不带 .h )并调用

 std :: locale :: global(std :: locale( )); 

这也将设置C库的语言环境。



另请注意 CString 类使用C库格式化字符串。因此,当将浮点数传递给 CString :: [Append] Format()时,也会使用上述调用设置的小数点。


< blockquote>转换小数比许多人想象的要复杂,因为有本地设置。有时候。有时,代表小数分隔符。该软件必须尊重该设置。通常是程序启动时使用的设置。



阅读 _tstof 更好地了解行为。最后是一些示例代码,演示了某些区域设置的明确使用。



考虑一些输入验证,例如将,更改为。在输入的字符串中,或​​者抱怨数据格式无效的消息。这是典型的UI背后代码......


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)))

解决方案

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

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

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().


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


这篇关于_Tstof问题,区域和语言中的小数变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆