mama
mama

Reputation: 3

Regex decimal to int

Im trying to read a decimal number from textbox and convert it in int to calculate with it.

The string looks like this +0.003_mm. Based on the regex online tools my pattern to capture the decimal number should look like this "d*(\d+.\d+" and the result would be 0.003

While testing this im was trying to display the 0.003 in a textbox called "mtw_textbox" thats why i convert it to a string in the end.

I have tried it this way and also as an Int32 for "mittelwert1" but i am getting an error saying that is the wrong format for " Int mittelwert1 = Convert.ToInt32(mtw1.Value);"

What am i doing wrong ?

       private void button1_Click_1(object sender, EventArgs e)
    {
        Regex regex = new Regex(@"d*(\d+\.\d+)");
        Match mtw1 = regex.Match(mw1_textbox.Text);

        if (mtw1.Success) { 
        Int mittelwert1 = Convert.ToInt32(mtw1.Value);
        string testen = mittelwert1.ToString();

Upvotes: 0

Views: 322

Answers (1)

Piotr Falkowski
Piotr Falkowski

Reputation: 1967

Converting "0.003" to integer will throw exactly this exception. This is beacause 0.003 is not an integer. Try converting to any floating point number, like float, double or decimal:

double mittelwert1 = Convert.ToDouble(mtw1.Value);

Upvotes: 2

Related Questions