如何将float转换为字符串,而不管区域设置如何? [英] How to convert a float to a string regardless of regional settings?

查看:226
本文介绍了如何将float转换为字符串,而不管区域设置如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的产品是针对葡萄牙的观众,逗号是小数点符号。我通常使用CString :: Format将数字输入到字符串,并考虑到计算机的区域设置。虽然通常这是一个好方法,但是在格式化SQL查询时遇到了问题,例如:

  CString szInsert; 
szInsert.Format(INSERT INTO Vertices(X,Y)VALUES(%f,%f),pt.X,pt.Y);

当值传递时,我得到这个字符串,这是一个不正确的查询:

  INSERT INTO顶点(X,Y)VALUES(3,56,4,67)

如何在这些字符串中强制使用小数点符号,而不必更改区域设置,也不必为每个浮点值设置专门的字符串? b
$ b注意:这是作为一个普通的问题,而不是一个SQL的。

解决方案

这是我做了什么。

$ p $ CString FormatQuery(LPCTSTR pszFormat,...)
{
CString szLocale = setlocale(LC_NUMERIC,NULL);
setlocale(LC_NUMERIC,English);

va_list args;
va_start(args,pszFormat);
CString szFormatted;
int nSize =(_vscprintf(pszFormat,args)+ 1)* sizeof(char);
_vsnprintf_s(szFormatted.GetBuffer(nSize),nSize,nSize,pszFormat,args);
szFormatted.ReleaseBuffer();
va_end(args);

setlocale(LC_NUMERIC,szLocale);
返回szFormatted;

$ / code>

您应该像 sprintf 。您必须 #include< locale.h> 才能正常工作。

我有点固执,所以我没有使用预处理语句/参数化查询。如果你有类似的问题,我建议你这样做。同时,如果你的问题不是SQL相关的,我的答案应该会有所帮助。



编辑:这是一个线程安全版本:



pre code> CString FormatQuery(LPCTSTR pszFormat,...)
{
_locale_t locale = _create_locale(LC_NUMERIC,English) ;

va_list args;
va_start(args,pszFormat);
CString szFormatted;
int nSize =(_vscprintf_l(pszFormat,locale,args)+ 1)* sizeof(char);
_vsnprintf_s_l(szFormatted.GetBuffer(nSize),nSize,nSize,pszFormat,locale,args);
szFormatted.ReleaseBuffer();
va_end(args);

return szFormatted;
}


My product is targeted to a Portuguese audience where the comma is the decimal symbol. I usually use CString::Format to input numbers into strings, and it takes into account the computer's regional settings. While in general this is a good approach, I'm having problems in formatting SQL queries, for instance:

CString szInsert;
szInsert.Format("INSERT INTO Vertices (X, Y) VALUES (%f, %f)", pt.X, pt.Y);

When values are passed I get this string which is an incorrect query:

INSERT INTO Vertices (X, Y) VALUES (3,56, 4,67)

How do I enforce the dot as the decimal symbol in these strings, without changing the regional settings and without having to make specialized strings for each float value?

Note: this is intended as a general question, not a SQL one.

解决方案

Here's what I did.

CString FormatQuery(LPCTSTR pszFormat, ...)
{
    CString szLocale = setlocale(LC_NUMERIC, NULL);
    setlocale(LC_NUMERIC, "English");

    va_list args;
    va_start(args, pszFormat);
    CString szFormatted;
    int nSize = (_vscprintf(pszFormat, args) + 1) * sizeof(char);
    _vsnprintf_s(szFormatted.GetBuffer(nSize), nSize, nSize, pszFormat, args);
    szFormatted.ReleaseBuffer();
    va_end(args);

    setlocale(LC_NUMERIC, szLocale);
    return szFormatted;
}

You should use it like sprintf. You must #include <locale.h> in order for it to work.

I'm a bit stubborn so I didn't use prepared statements/parametrized queries. If you have a similar problem, I suggest you do that. Meanwhile, if your problem is not SQL-related, my answer should help.

Edit: Here's a thread safe version:

CString FormatQuery(LPCTSTR pszFormat, ...)
{
    _locale_t locale = _create_locale(LC_NUMERIC, "English");

    va_list args;
    va_start(args, pszFormat);
    CString szFormatted;
    int nSize = (_vscprintf_l(pszFormat, locale, args) + 1) * sizeof(char);
    _vsnprintf_s_l(szFormatted.GetBuffer(nSize), nSize, nSize, pszFormat, locale, args);
    szFormatted.ReleaseBuffer();
    va_end(args);

    return szFormatted;
}

这篇关于如何将float转换为字符串,而不管区域设置如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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