我怎样才能将字符串转换为双倍?我得到了例外

时间:2017-06-03 23:37:39

标签: c#

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

我得到例外:

FormatException:未知的char:,

但是secondNumber中的字符串中没有任何char / s:0.87142591

这是transform:matrix中的整个字符串(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)

我提取前两个数字:0.49052715和0.87142591然后尝试将它们转换为double。但得到例外。

包含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;
        }
    }

3 个答案:

答案 0 :(得分: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 :(得分: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);

答案 2 :(得分: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;
  }
 }