如何将字符串转换为双精度?我收到异常


for(int i = 0; i < Rect.rectangles.Count; i++)
{
if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "")
{
string firstNumber = Rect.rectangles[i].transform.Substring(18, 10);
string secondNumber = Rect.rectangles[i].transform.Substring(7, 10);
double a = Convert.ToDouble(firstNumber);
double b = Convert.ToDouble(secondNumber);
}
}

第一个数字是 0.49052715,a 的 Convert.ToDouble 很好。 但是当它进入线时:

double b = Convert.ToDouble(secondNumber);

我收到异常:

格式异常: 未知字符: ,

但是秒中根本没有任何字符数字是:0.87142591

这是转换中的整个字符串:矩阵(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)

我正在提取前两个数字:0.49052715 和 0.87142591,然后尝试将它们转换为双精度。但是得到例外。

包含 Rect.rectangles 定义的代码:

private void ParseSvgMap()
{
XDocument document = XDocument.Load(@"C:UsersmysvgDocumentsmy.svg");
Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect()
{
style = Rect.GetStyle((string)x.Attribute("style")),
id = (string)x.Attribute("id"),
width = (double)x.Attribute("width"),
height = (double)x.Attribute("width"),
x = (double?)x.Attribute("width"),
y = (double?)x.Attribute("width"),
transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform")
}).ToList();
for(int i = 0; i < Rect.rectangles.Count; i++)
{
if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "")
{
string firstNumber = Rect.rectangles[i].transform.Substring(18, 10);
string secondNumber = Rect.rectangles[i].transform.Substring(7, 10);
double a = Convert.ToDouble(firstNumber);
double b = Convert.ToDouble(secondNumber);
float degree = FindDegree(a, b);
}
}
}
public static float FindDegree(double a, double b)
{
float value = (float)((System.Math.Atan2(a, b) / System.Math.PI) * 180f);
if (value < 0) value += 360f;
return value;
}
public class Rect
{
public static List<Rect> rectangles { get; set; }
public Dictionary<string, string> style { get; set; }
public string id { get; set; }
public double width { get; set; }
public double height { get; set; }
public double? x { get; set; }
public double? y { get; set; }
public string transform { get; set; }
public double degree { get; set; }
public static Dictionary<string, string> GetStyle(string styles)
{
string pattern = @"(?'name'[^:]+):(?'value'.*)";
string[] splitArray = styles.Split(new char[] { ';' });
Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern))
.GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
return style;
}
}

.Substring(18, 10)这样的代码是无可救药的脆弱,并且(正如您所发现的)是等待发生的事故。不保证数字的长度相同。只需查看您提供的示例数据即可。

在尝试解析字符串之前,您应该使用类似正则表达式的东西从字符串中提取数字。

var regex  = new Regex(@"matrix((?<num>-?(?:d|.)+)(?:,(?<num>-?(?:d|.)+))*)");
var data   = "matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)";
var values = regex
.Matches(data)
.Cast<Match>()
.SelectMany(m => m.Groups["num"]
.Captures
.Cast<Capture>()
.Select(c=>c.Value))
.Select(double.Parse)
.ToList();

然后用values[0]values[1]挑选出您想要的数字。

它注定要失败。 我会使用以下正则表达式:

string test = "matrix(0.1234,0.23233433,0.34,-3343,-3.33)";
Regex reg = new Regex("[-]?[0-9]+(\.[0-9]+)?");
MatchCollection coll = reg.Matches(test);

您可以按如下方式编写方法:

public double fixString(string incoming)
{
Double a;
if(incoming.Contains(','))
{
string fixedNum = incoming.Remove(incoming.IndexOf(','));
a = Convert.ToDouble(fixedNum);
return a;
}
else
{
a = Convert.ToDouble(incoming);
return a;
}
}

最新更新