2017-06-03 115 views
-1
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及的是罚款Convert.ToDouble。 但是,当它变得前往路线:如何将字符串转换为double?我得到异常

double b = Convert.ToDouble(secondNumber); 

我得到异常:

出现FormatException:未知字符:,

但没有任何字符/秒在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:\Users\mysvg\Documents\my.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; 
     } 
    } 
+0

你肯定'secondNumber'不包含无效字符?你有没有在调试器中看到它的价值?因为C#(通常)不会说谎 – niceman

+0

向我们展示'Rect.rectangles'的定义。 –

+2

在@niceman所说的调试器中检查'secondNumber'的值。 – MiOnIs

回答

2

代码像.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]的数字。

+1

似乎有点像对我来说矫枉过正。 – DaOnlyOwner

+1

@DaOnlyOwner可能。我喜欢我的代码来验证尽可能多的假设,可能的,但我可以看到你的POV – spender

+0

我明白;) – DaOnlyOwner

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); 
+0

减号('-')的迹象? – spender

0

你可以写的线沿线的一个方法:

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; 
    } 
} 
相关问题