Pregunta

this is my code;

string a="11.4";

int b,c;

b=2;

c= convert.toint32(a) * b

I get this error;

Input string was not in a correct format

how can i convert "a"?

¿Fue útil?

Solución

Well a is just not an integer value - you could use Convert.ToDouble() instead. To guard against parsing errors in case that is a possibility use double.TryParse() instead:

string a = "11.4";
double d;

if (double.TryParse(a, out d))
{
    //d now contains the double value
}

Edit:

Taking the comments into account, of course it is always best to specify the culture settings. Here an example using culture-indepenent settings with double.TryParse() which would result in 11.4 as result:

if (double.TryParse(a, NumberStyles.Number, CultureInfo.InvariantCulture, out d))
{
    //d now contains the double value
}

Otros consejos

A very first glance, the number literal "11.4" is not a actual "int". Try some other converting format, such as ToDouble()

I have tried following code in C# for your reference.

        string a = "11.4";
        double num_a = Convert.ToDouble(a);
        int b = 2;
        double ans = num_a * b;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top