如何将两个整数相除以获得双精度数?

发布于 2024-07-15 23:51:25 字数 24 浏览 3 评论 0原文

如何将两个整数相除以获得双精度数?

How do I divide two integers to get a double?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

萌酱 2024-07-22 23:51:25

您想要转换数字:

double num3 = (double)num1/(double)num2;

注意:如果 C# 中的任何参数是 double,则使用 double 除法,结果是 double >。 因此,以下内容也可以:

double num3 = (double)num1/num2;

有关详细信息,请参阅:

Dot Net Perls

You want to cast the numbers:

double num3 = (double)num1/(double)num2;

Note: If any of the arguments in C# is a double, a double divide is used which results in a double. So, the following would work too:

double num3 = (double)num1/num2;

For more information see:

Dot Net Perls

挖鼻大婶 2024-07-22 23:51:25

补充@NoahD的答案

要获得更高的精度,您可以转换为十进制:

(decimal)100/863
//0.1158748551564310544611819235

或者:

Decimal.Divide(100, 863)
//0.1158748551564310544611819235

Double 表示分配 64 位,而十进制使用 128

(double)100/863
//0.11587485515643106

深入解释“精度”

有关二进制中的浮点表示及其精度的更多详细信息,请参阅请参阅 Jon Skeet 的这篇文章,他在其中讨论了浮动双打这个,他在其中谈论了小数点

Complementing the @NoahD's answer

To have a greater precision you can cast to decimal:

(decimal)100/863
//0.1158748551564310544611819235

Or:

Decimal.Divide(100, 863)
//0.1158748551564310544611819235

Double are represented allocating 64 bits while decimal uses 128

(double)100/863
//0.11587485515643106

In depth explanation of "precision"

For more details about the floating point representation in binary and its precision take a look at this article from Jon Skeet where he talks about floats and doubles and this one where he talks about decimals.

祁梦 2024-07-22 23:51:25

将整数转换为双精度数。

cast the integers to doubles.

微暖i 2024-07-22 23:51:25

首先将其中一个转换为双精度。 此表格适用于多种语言:

 real_result = (int_numerator + 0.0) / int_denominator

Convert one of them to a double first. This form works in many languages:

 real_result = (int_numerator + 0.0) / int_denominator
罪歌 2024-07-22 23:51:25
var firstNumber=5000,
secondeNumber=37;

var decimalResult = decimal.Divide(firstNumber,secondeNumber);

Console.WriteLine(decimalResult );
var firstNumber=5000,
secondeNumber=37;

var decimalResult = decimal.Divide(firstNumber,secondeNumber);

Console.WriteLine(decimalResult );
又怨 2024-07-22 23:51:25
var result = decimal.ToDouble(decimal.Divide(5, 2));
var result = decimal.ToDouble(decimal.Divide(5, 2));
过期以后 2024-07-22 23:51:25

评论中接受的答案有一个区别,这似乎值得在单独的答案中强调。

正确的代码:

double num3 = (double)num1/(double)num2;

与转换整数除法的结果不同:

double num3 = (double)(num1/num2);

给定 num1 = 7 和 num2 = 12:

正确的代码将导致 num3 = 0.5833333

转换整数除法的结果将导致 num3 = 0.00

In the comment to the accepted answer there is a distinction made which seems to be worth highlighting in a separate answer.

The correct code:

double num3 = (double)num1/(double)num2;

is not the same as casting the result of integer division:

double num3 = (double)(num1/num2);

Given num1 = 7 and num2 = 12:

The correct code will result in num3 = 0.5833333

Casting the result of integer division will result in num3 = 0.00

三生殊途 2024-07-22 23:51:25

最简单的方法是为整数添加小数位。

前任。:

var v1 = 1 / 30 //the result is 0
var v2 = 1.00 / 30.00 //the result is 0.033333333333333333

The easiest way to do that is adding decimal places to your integer.

Ex.:

var v1 = 1 / 30 //the result is 0
var v2 = 1.00 / 30.00 //the result is 0.033333333333333333
酒中人 2024-07-22 23:51:25

我已经浏览了大部分答案,我很确定这是无法实现的。 无论你尝试将两个 int 分成 double 或 float 都不会发生。
但是你有大量的方法来进行计算,只需将它们转换为 float 或 double 即可,然后计算就可以了。

I have went through most of the answers and im pretty sure that it's unachievable. Whatever you try to divide two int into double or float is not gonna happen.
But you have tons of methods to make the calculation happen, just cast them into float or double before the calculation will be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文