Pra*_*hal 0 c# double truncate
我有一个功能,我需要传递一个double.要调用该函数,我使用以下代码: -
static int Main()
{
double d = 1/7 ;
Console.WriteLine("The value of d is {0}", d) ;
calc(d) ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
以下程序的输出是
d的值为0
为什么这样,为什么C#截断小数前面的部分,尽管在双倍中存储1/7?
Mit*_*eat 11
一个int由分int使用整数截断.
使用:
static int Main()
{
double d = 1.0 / 7 ;
//^^ or d = 1.0 / 7.0
Console.WriteLine("The value of d is {0}", d) ;
calc(d) ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
将分子或分母(或两者)提升为浮点类型可将分割结果提升为浮点类型.
参考文献:
| 归档时间: |
|
| 查看次数: |
301 次 |
| 最近记录: |