9

我对 String.Format 有疑问。除了第一个整数之外,以下代码正确格式化字符串。当前文化设置为伊拉克阿拉伯语(ar-IQ):

int currentItem= 1;
string of= "من";
int count = 2;
string formatted = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", currentItem, of, count);

文本从右到左格式化,2 转换为阿拉伯数字,但 1 不是。

有任何想法吗?

4

3 回答 3

3

转换数值的默认行为是“上下文”,这基本上意味着如果数字以阿拉伯语开头,则以阿拉伯语(或另一个“非拉丁”字符)显示,如果不是,则以“标准”欧洲字符显示数字。

你可以很容易地改变这种行为:

var culture = CultureInfo.CurrentCulture;
culture.NumberFormat.DigitSubstitution = DigitShapes.NativeNational; // Always use native characters
string formatted = string.Format(culture, "{0:d}{1:d}{2:d}", currentItem, of, count);

这应该可以按您的预期工作 - 有关MSDN的更多详细信息。

于 2010-06-16T16:41:06.040 回答
1

我无法得到其他任何一个答案。这对我有用:

string sOriginal = "1 of 2";
var ci = new CultureInfo("ar-IQ", false);
var nfi = ci.NumberFormat;
string sNative = ReplaceWesternDigitsWithNativeDigits(sOriginal, nfi).Replace("of", "من");

...

private static string ReplaceWesternDigitsWithNativeDigits(string s, NumberFormatInfo nfi)
{
    return s.Replace("0", nfi.NativeDigits[0])
        .Replace("1", nfi.NativeDigits[1])
        .Replace("2", nfi.NativeDigits[2])
        .Replace("3", nfi.NativeDigits[3])
        .Replace("4", nfi.NativeDigits[4])
        .Replace("5", nfi.NativeDigits[5])
        .Replace("6", nfi.NativeDigits[6])
        .Replace("7", nfi.NativeDigits[7])
        .Replace("8", nfi.NativeDigits[8])
        .Replace("9", nfi.NativeDigits[9]);
}
于 2020-05-14T16:58:12.183 回答
0
var culture = CultureInfo.CurrentCulture;
culture.NumberFormat.DigitSubstitution = DigitShapes.NativeNational;

不起作用,但以下工作:

var culture = new CultureInfo("ar-SA");
culture.NumberFormat = new NumberFormatInfo();
Thread.CurrentThread.CurrentCulture = culture;

谢谢提示!!!

于 2011-01-24T12:07:48.140 回答