我需要将表数据库输入的字符串转换为 C# .NET 4 中的整数值,并尝试了受此链接启发的代码:
int i;
string Entry_Level = Convert.ToInt32("2,45");
i = Convert.ToInt32(Entry_Level);
Run Code Online (Sandbox Code Playgroud)
但我有这个错误:
编译器错误消息:CS0029:无法将类型“int”隐式转换为“string”
编辑
解决方法:
decimal i;
string Entry_Level = "2,45";
i = Convert.ToDecimal(Entry_Level);
Response.Write(i.ToString());
Response.End();
Run Code Online (Sandbox Code Playgroud)
输出结果是 2,45,非常感谢!
string Entry_Level = Convert.ToInt32("2,45");
Run Code Online (Sandbox Code Playgroud)
应该
string Entry_Level = "2,45";
Run Code Online (Sandbox Code Playgroud)
为什么不这样做呢:
int i = 2,45;
Run Code Online (Sandbox Code Playgroud)
但由于这不是整数,您将需要内置十进制类型之一:
/* 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
Run Code Online (Sandbox Code Playgroud)
有关十进制与双精度的更多信息,请参阅此线程。
| 归档时间: |
|
| 查看次数: |
123436 次 |
| 最近记录: |