简单的方法来获取使用默认值填充的NUMBERFMT? [英] Easy way to get NUMBERFMT populated with defaults?

查看:117
本文介绍了简单的方法来获取使用默认值填充的NUMBERFMT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows API GetNumberFormatEx 格式化一些数字,以使用适合当前用户的本地化选项进行显示(例如,确保它们在正确的位置具有正确的分隔符).当您想要确切的用户默认设置时,这很简单.

I'm using the Windows API GetNumberFormatEx to format some numbers for display with the appropriate localization choices for the current user (e.g., to make sure they have the right separators in the right places). This is trivial when you want exactly the user default.

但是在某些情况下,有时我必须重写基数分隔符后的位数.这就需要提供一个 NUMBERFMT 结构.我想做的是调用一个API,该API返回使用用户适当的默认值填充的NUMBERFMT,然后仅覆盖我需要更改的字段.但是似乎没有API可以获取默认值.

But in some cases I sometimes have to override the number of digits after the radix separator. That requires providing a NUMBERFMT structure. What I'd like to do is to call an API that returns the NUMBERFMT populated with the appropriate defaults for the user, and then override just the fields I need to change. But there doesn't seem to be an API to get the defaults.

当前,我一遍又一遍地调用GetLocaleInfoEx,然后将该数据转换为NUMBERFMT所需的形式.

Currently, I'm calling GetLocaleInfoEx over and over and then translating that data into the form NUMBERFMT requires.

NUMBERFMT fmt = {0};
::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT,
                  LOCALE_IDIGITS | LOCALE_RETURN_NUMBER,
                  reinterpret_cast<LPWSTR>(&fmt.NumDigits),
                  sizeof(fmt.NumDigits)/sizeof(WCHAR));
::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT,
                  LOCALE_ILZERO | LOCALE_RETURN_NUMBER,
                  reinterpret_cast<LPWSTR>(&fmt.LeadingZero),
                  sizeof(fmt.LeadingZero)/sizeof(WCHAR));
WCHAR szGrouping[32] = L"";
::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SGROUPING, szGrouping,
                  ARRAYSIZE(szGrouping));
if (::lstrcmp(szGrouping, L"3;0") == 0 ||
    ::lstrcmp(szGrouping, L"3") == 0
) {
    fmt.Grouping = 3;
} else if (::lstrcmp(szGrouping, L"3;2;0") == 0) {
    fmt.Grouping = 32;
} else {
    assert(false);  // unexpected grouping string
}
WCHAR szDecimal[16] = L"";
::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SDECIMAL, szDecimal,
                  ARRAYSIZE(szDecimal));
fmt.lpDecimalSep = szDecimal;
WCHAR szThousand[16] = L"";
::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_STHOUSAND, szThousand,
                  ARRAYSIZE(szThousand));
fmt.lpThousandSep = szThousand;
::GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT,
                  LOCALE_INEGNUMBER | LOCALE_RETURN_NUMBER,
                  reinterpret_cast<LPWSTR>(&fmt.NegativeOrder),
                  sizeof(fmt.NegativeOrder)/sizeof(WCHAR));

是否没有已经执行此操作的API?

Isn't there an API that already does this?

推荐答案

上周我刚刚编写了一些代码来做到这一点. las,似乎没有GetDefaultNumberFormat(LCID lcid,NUMBERFMT * fmt)函数;您必须先开始自己编写.附带一提,分组字符串具有定义明确的格式,可以轻松地对其进行解析;您当前的代码对于"3"(应为30)是错误的,并且显然在更多奇特的分组上将失败(尽管实际上这可能不是很多问题).

I just wrote some code to do this last week. Alas, there does not seem to be a GetDefaultNumberFormat(LCID lcid, NUMBERFMT* fmt) function; you will have to write it yourself as you've already started. On a side note, the grouping string has a well-defined format that can be easily parsed; your current code is wrong for "3" (should be 30) and obviously will fail on more exotic groupings (though this is probably not much of a concern, really).

这篇关于简单的方法来获取使用默认值填充的NUMBERFMT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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