gpt4 book ai didi

c# - 编译器错误消息 : CS0029: Cannot implicitly convert type 'int' to 'string'

转载 作者:行者123 更新时间:2023-12-05 01:47:37 25 4
gpt4 key购买 nike

我需要在 C# .NET 4 中将表数据库输入的 String 转换为 Integer value 并尝试了受此启发的代码 Link :

    int i;
string Entry_Level = Convert.ToInt32("2,45");
i = Convert.ToInt32(Entry_Level);

但是我有这个错误:

Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'

编辑

解决方法:

    decimal i;
string Entry_Level = "2,45";
i = Convert.ToDecimal(Entry_Level);

Response.Write(i.ToString());
Response.End();

在输出中我有 2,45,非常感谢!

最佳答案

string Entry_Level = Convert.ToInt32("2,45");

应该是

string Entry_Level = "2,45";

为什么不这样做:

int i = 2,45;

但由于这不是整数,因此您需要一种内置的小数类型:

/* use this when precision matters a lot, for example when this is a unit price or a percentage value that will be multiplied with big numbers */
decimal i = 2.45


/* use this when precision isn't the most important part.
It's still really precise, but you can get in trouble when dealing with really small or really big numbers.
Doubles are fine in most cases.*/
double i = 2.45

参见 this thread for more information about decimal vs double .

关于c# - 编译器错误消息 : CS0029: Cannot implicitly convert type 'int' to 'string' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com