使用 int.parse 配合 numberstyles.allowfullwidth 即可将全角数字字符串转换为整数。需要引入 system.globalization 命名空间,并在解析时指定样式标志,这样解析器就能识别全角字符并返回对应的整数值。

NumberStyles 默认行为与全角支持的区别
如果不指定样式参数,int.Parse 默认只接受半角数字。直接传入全角字符串会抛出 FormatException。必须显式启用 AllowFullWidth 才能处理这类输入。

using System;
using System.Globalization;
class Program
{
static void Main()
{
string fullWidthNum = "123";
// 正确转换
int result = int.Parse(fullWidthNum, NumberStyles.AllowFullWidth);
Console.WriteLine(result); // 输出: 123
// 错误示范:默认风格不支持全角
try
{
int error = int.Parse(fullWidthNum);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message);
}
}
}
如何处理包含非数字字符的全角混合字符串?
如果字符串中包含全角空格或其他符号,仅靠 AllowFullWidth 不够,需组合其他样式如 AllowLeadingWhite 或 AllowTrailingWhite。若存在非法字符,解析仍会失败。

using System;
using System.Globalization;
class Program
{
static void Main()
{
string mixedStr = " 123 ";
int result = int.Parse(mixedStr, NumberStyles.AllowFullWidth | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
Console.WriteLine(result); // 输出: 123
}
}
全角零值“0”的解析结果是什么?
全角零 “0” 在启用 AllowFullWidth 后会被正确解析为整数 0,不会报错。这是符合 Unicode 标准的正常行为。











