I am trying to convert decimal and its converting but I want my output with decimal values for example.
我试图转换十进制和它的转换,但我希望我的输出与十进制值,例如。
If I convert 1 then I am expecting 1.00
如果我转换1然后我期待1.00
Here is my code
这是我的代码
decimal AL;
switch (CurrentScore)
{
case 1:
AL = Convert.ToDecimal(1.00);
break;
case 2:
AL = Convert.ToDecimal(1.5);
break;
case 3:
AL = Convert.ToDecimal(2.00);
break;
default:
AL = Convert.ToDecimal(0.00);
break;
}
return AL;
but this always return like integer.
但这总是像整数一样返回。
For example if my currentscore is "1" then I am expecting to return AL = 1.00 but it always returns "1".
例如,如果我的currecore是“1”,那么我期望返回AL = 1.00,但它总是返回“1”。
1 个解决方案
#1
Decimals (and other numeric types) don't have formatting embedded. 1 == 1.0 == 1.00
.
小数(和其他数字类型)没有嵌入格式。 1 == 1.0 == 1.00。
If you want to display them formatted with 2 decimal places, then do something like this:
如果要显示格式为2位小数的格式,请执行以下操作:
Console.WriteLine(AL.ToString("0.00"));
Also note that you don't have to use Convert.ToDecimal
here. You can just say AL = 1.00
.
另请注意,您不必在此处使用Convert.ToDecimal。你可以说AL = 1.00。
#1
Decimals (and other numeric types) don't have formatting embedded. 1 == 1.0 == 1.00
.
小数(和其他数字类型)没有嵌入格式。 1 == 1.0 == 1.00。
If you want to display them formatted with 2 decimal places, then do something like this:
如果要显示格式为2位小数的格式,请执行以下操作:
Console.WriteLine(AL.ToString("0.00"));
Also note that you don't have to use Convert.ToDecimal
here. You can just say AL = 1.00
.
另请注意,您不必在此处使用Convert.ToDecimal。你可以说AL = 1.00。