C# double not working as expected [duplicate]

2020-05-10 09:29发布

I understand that a double is a decimal. In the following program the output is 1 even though I thought it would be 1.05 repeating.

static void Main (string[] args)
{
double d = 19 / 18;
Console.WriteLine(d);
Console.ReadKey();
}

Am I misunderstanding double?

标签: c# double
1条回答
Juvenile、少年°
2楼-- · 2020-05-10 09:54

You are misunderstanding integer math.

Integer-19 / Integer-18 results in an Integer with value 1.

(that you assign the value to a double is not relevant. The calculation results in an Integer).

To fix it, use:

double d = 19.0 / 18.0;
查看更多
登录 后发表回答