本文介绍了C# int 和非英文数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C# 是否可以int
数据类型保存文化特定数字,如东方阿拉伯语 数字?例如.123"
将是
Can C# int
data type hold culture specific numbers like Eastern Arabic numbers? E.g. "123"
will be
١٢٣
我正在使用 SoapUI 发送请求和接收响应.Web 服务是用 c# 编写的.
I’m working with SoapUI to send requests and receive responses.The web service is written in c#.
但是,当我在 Soap UI 中输入这些东方阿拉伯数字时,它说
However when I enter these Eastern Arabic numbers in Soap UI, it says
无法解析该值".
不清楚是 Soap UI 问题还是 c# 问题.
It’s not clear if it’s Soap UI issue or c# issue.
有人可以帮忙吗?
感谢您的回答!
推荐答案
您可以尝试使用 char.GetNumericValue 将文化特定数字(例如波斯语)转换为常见的0..9
:
You can try using char.GetNumericValue to convert culture specific digits (e.g. Persian) into common 0..9
:
private static bool TryParseAnyCulture(string value, out int result) {
result = default(int);
if (null == value)
return false;
StringBuilder sb = new StringBuilder(value.Length);
foreach (char c in value) {
double d = char.GetNumericValue(c);
// d < 0 : character is not a digit, like '-'
// d % 1 != 0 : character represents some fraction, like 1/2
if (d < 0 || d % 1 != 0)
sb.Append(c);
else
sb.Append((int)d);
}
return int.TryParse(sb.ToString(), out result);
}
演示:
string value = "١٢٣"; // Eastern Arabic Numerals (0..9 are Western)
Console.Write(TryParseAnyCulture(value, out var result) ? $"{result}" : "???");
结果:
123
这篇关于C# int 和非英文数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!