How can I parse only the float number from the string?

前端 未结 3 1203
悲&欢浪女
悲&欢浪女 2021-01-21 19:27
foreach (object item in listBox1.SelectedItems)
{
    string curItem = item.ToString();
    var parts = curItem.Split(\"{}XY=, \".ToCharArray(), StringSplitOptions.Remov         


        
3条回答
  •  时光取名叫无心
    2021-01-21 19:47

    try this

     foreach (object item in listBox1.SelectedItems)
            {
                string curItem = item.ToString();
                var parts = curItem.Split("{}XY=, ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                var xCoord = CultureCheck(parts[0]);
                var yCoord = CultureCheck(parts[1]);
                var point = new PointF(xCoord, yCoord);
                CloudEnteringAlert.pointtocolor.Add(point);
                pictureBox1.Invalidate();
            }       
    
    
        private double CultureCheck(string input)
        {
            CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            ci.NumberFormat.CurrencyDecimalSeparator = ".";
           return double.Parse(input, NumberStyles.Any, ci);
        }
    

提交回复
热议问题