0

这是我的代码;

string a="11.4";

int b,c;

b=2;

c= convert.toint32(a) * b

我收到此错误;

输入字符串的格式不正确

我怎样才能转换“a”?

4

2 回答 2

4

Wella不是一个整数值 - 你可以使用它Convert.ToDouble()。为了防止在可能的情况下出现解析错误,请double.TryParse()改用:

string a = "11.4";
double d;

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

编辑:

考虑到评论,当然最好指定文化设置。这是一个使用文化独立设置的示例double.TryParse(),结果将11.4是:

if (double.TryParse(a, NumberStyles.Number, CultureInfo.InvariantCulture, out d))
{
    //d now contains the double value
}
于 2012-01-26T23:51:21.723 回答
1

乍一看,数字文字“11.4”不是实际的“int”。尝试其他一些转换格式,例如 ToDouble()

我在 C# 中尝试了以下代码供您参考。

        string a = "11.4";
        double num_a = Convert.ToDouble(a);
        int b = 2;
        double ans = num_a * b;
于 2012-01-26T23:51:55.743 回答