C#是双未按预期 [英] C# double not working as expected

查看:85
本文介绍了C#是双未按预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,双是一个小数。
在下面的程序输出为1,即使我认为这将是1.05重复。

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?

推荐答案

您误解整数运算。

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).

要修复它,使用:

double d = 19.0 / 18.0;

这篇关于C#是双未按预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆