c# - String.Format 没有正确转换阿拉伯语的整数

我对 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 不是。

有什么想法吗?

最佳答案

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

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

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 .

https://stackoverflow.com/questions/3055195/

相关文章:

android - EditText 提示随重力消失

php - 为什么 PHP 回显的文本会丢失其格式?

c# - Html.HiddenFor 在 ASP.NET 中错误地格式化 DateTime

awk - 带有 awk 的 pretty-print 表

javascript - 如何在 Eclipse 的 Javascript 源代码中设置数组元素的缩

css - 防止eclipse破坏CSS格式

perl - 使用 Perl 进行格式化输出的最佳方式

c# - 支持自定义数字类型的 ToString(字符串格式)

vim - 在 Vim 中将光标移过长的软环绕行

python - 在空格或连字符上拆分?