简体   繁体   English

C#简单划分问题

[英]C# simple divide problem

I have this: 我有这个:

 double result = 60 / 23;

In my program, the result is 2, but correct is 2,608695652173913. 在我的程序中,结果是2,但正确的是2,608695652173913。 Where is problem? 哪里有问题?

60 and 23 are integer literals so you are doing integer division and then assigning to a double. 60和23是整数文字,因此您进行整数除法然后分配给double。 The result of the integer division is 2. 整数除法的结果是2。

Try 尝试

double result = 60.0 / 23.0;

Or equivalently 或者等价

double result = 60d / 23d;

Where the d suffix informs the complier that you meant to write a double literal. d后缀通知编译器你打算写一个双字面。

You can use any of the following all will give 2.60869565217391 : 你可以使用以下任何一个都给2.60869565217391

 double result = 60 / 23d;  
 double result = 60d / 23;  
 double result = 60d/ 23d;  
 double result = 60.0 / 23.0;   

But

double result = 60 / 23;  //give 2

Explanation: 说明:

if any of the number is double it will give a double 如果任何一个数字是双倍 ,它将给出一倍


EDIT: 编辑:

Documentation 文档

The evaluation of the expression is performed according to the following rules: 表达式的评估根据以下规则执行:

  • If one of the floating-point types is double, the expression evaluates to double (or bool in the case of relational or Boolean expressions). 如果其中一个浮点类型为double,则表达式求值为double(或者在关系或布尔表达式的情况下为bool)。

  • If there is no double type in the expression, it evaluates to float (or bool in the case of relational or Boolean expressions). 如果表达式中没有double类型,则计算为float(或者在关系表达式或布尔表达式的情况下为bool)。

It will work 它会工作

double result = (double)60 / (double) 23;

Or equivalently 或者等价

double result = (double)60 /  23;

(双)60/23

Haven't used C# for a while, but you are dividing two integers, which as far as I remember makes the result an integer as well. 暂时没有使用过C#,但你要划分两个整数,据我所记得的那样,结果也是一个整数。

You can force your number literals to be doubles by adding the letter "d", likes this: 您可以通过添加字母“d”强制您的数字文字加倍,喜欢这样:

double result = 60d / 23d;

double result = 60.0 / 23.0;

It is best practice to correctly decorate numerals for their appropriate type. 最佳做法是正确装饰适当类型的数字。 This avoids not only the bug you are experiencing, but makes the code more readable and maintainable. 这不仅可以避免您遇到的错误,还可以使代码更具可读性和可维护性。

double x = 100d;
single x = 100f;
decimal x = 100m;

convert the dividend and divisor into double values, so that result is double 将被除数和除数转换为double值,因此结果是double
double res= 60d/23d; double res = 60d / 23d;

To add to what has been said so far... 60/23 is an operation on two constants. 添加到目前为止所说的内容... 60/23是对两个常量的操作。 The compiler recognizes the result as a constant and pre-computes the answer. 编译器将结果识别为常量并预先计算答案。 Since the operation is on two integers, the compiler uses an integer result The integer operation of 60/23 has a result of 2; 由于操作是两个整数,编译器使用整数结果60/23的整数运算结果为2; so the compiler effective creates the following code: 所以编译器有效创建以下代码:

double result = 2;

As has been pointed out already, you need to tell the compiler not to use integers, changing one or both of the operands to non-integer will get the compiler to use a floating-point constant. 正如已经指出的那样,您需要告诉编译器不要使用整数,将一个或两个操作数更改为非整数将使编译器使用浮点常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM