简体   繁体   English

输入字符串的格式不正确,无法从标签中检索值

[英]Input string was not in a correct format retriving value from label

string labeldistance = lbldistance.Text;
string output = Regex.Match(labeldistance, @"\d+").Value;
double labeldistance1 = Convert.ToDouble(output);
double carMilege = 10;
double cost = 70;
lblResult.Text = ((labeldistance1/carMilege)*cost).ToString();

retrieving value from label and label contains both string and integer 从标签检索值,并且标签包含字符串和整数

You Need to first make your string an int 您需要先将您的字符串设置为int

   string labeldistance = lbldistance.Text;
   labeldistance.ToInt32();
   string output = Regex.Match(labeldistance, @"\d+").Value;
   double labeldistance1 = Convert.ToDouble(output);

EDIT: If you need it to a float just do the same thing, just 编辑:如果您需要它浮动,只需执行相同的操作,只是

  labeldistance.ToFloat();

Regarding the error: 关于错误:

Input string was not in a correct format retriving value from label 输入字符串的格式不正确,无法从标签中检索值

This error appears when the input string to Convert.ToDouble() contains no numbers so in your program, output definitely does not have any numbers. Convert.ToDouble()的输入字符串不包含数字时,会出现此错误,因此在您的程序中, output绝对没有任何数字。

Convert.ToDouble(""); // throws System.FormatException: Input string was 
                      // not in a correct format.

Convert.ToDouble("48.1") // works fine

So you should debug, set a breakpoint and check if lbldistance.Text really contains '48.1km', and then if output contains 48. 因此,您应该调试,设置断点并检查lbldistance.Text确实包含“ lbldistance.Text ”,然后output包含48。

Extracting the number using regex: 使用正则表达式提取数字:

As per your example, it looks like you want to extract 48.1 using the double variable. 按照您的示例,您似乎想要使用double变量提取48.1。 For that you might want to tweak you regex like this: 为此,您可能想要像这样调整正则表达式:

\d+(.\d+)?

This will capture numbers containing decimals too. 这也将捕获包含小数的数字。 Here's a working demo using this regex and parts of your code: 这是使用此正则表达式和部分代码的工作示例:

string labeldistance = "48.1km";
string output = Regex.Match(labeldistance, @"\d+(.\d+)?").Value;
double labeldistance1 = Convert.ToDouble(output);
Console.WriteLine(labeldistance1);
// Outputs 48.1

Dotnet Fiddle 点网小提琴

So if you can ensure lbldistance.Text really contains 48.1km then using the above regex you should be able to get your desired output. 因此,如果您可以确保lbldistance.Text确实包含48.1km,那么使用上述正则表达式,您应该能够获得所需的输出。

Hope this helps! 希望这可以帮助!

Always sanitize input you get from the user: 始终清理从用户那里获得的输入:

using System;
using System.Text.RegularExpressions;

namespace StackOverflow_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            const string input = "Label: +1,234.56";
            string sanitized = Regex.Match(input, @"[0-9\.\+\-\,]+").Value; // filters out everything except digits (0-9), decimal points (.), signs (+ or -), and commas (,)
            double distance = Convert.ToDouble(sanitized);

            Console.WriteLine($"Input => \t\t{input}");             // Label: +1,234.56
            Console.WriteLine($"Standardized => \t{sanitized}");    // +1,234.56
            Console.WriteLine($"Distance => \t\t{distance}");       // 1234.56
            Console.ReadKey();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM