在 C# 中使除法返回双倍的最佳做法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/488942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best practice to make division return double in C#
提问by Element
In c# when you want to divide the result of a method such as below, what is the best way to force it to return a double value rather than the default integer.
在 c# 中,当您想对如下方法的结果进行除法时,强制它返回双精度值而不是默认整数的最佳方法是什么。
(int)Math.Ceiling((double)(System.DateTime.DaysInMonth(2009, 1) / 7));
As you can see I need the division to return a double so I can use the ceiling function.
如您所见,我需要除法返回双精度值,以便我可以使用天花板函数。
采纳答案by Konrad Rudolph
A division of two int
numbers returns an int
, truncating any decimal points. This is generally true for other data types as well: arithmetic operations don't change the type of their operands.
两个int
数字的除法返回一个int
,截断任何小数点。这通常也适用于其他数据类型:算术运算不会改变其操作数的类型。
To enforce a certain return type, you must therefore convert the operands appropriately. In your particular case, it's actually sufficient to convert one of the operators to double
: that way, C# will perform the conversion for the other operand automatically.
因此,要强制执行某种返回类型,您必须适当地转换操作数。在您的特定情况下,将其中一个运算符转换为实际上就足够了double
:这样,C# 将自动执行另一个操作数的转换。
You've got the choice: You can explicitly convert either operand. However, since the second operand is a literal, it's better just to make that literal the correct type directly.
您可以选择:您可以显式转换任一操作数。但是,由于第二个操作数是一个字面量,最好直接使该字面量成为正确的类型。
This can either be done using a type suffix (d
in the case of double
) or to write a decimal point behind it. The latter way is generally preferred. in your case:
这可以使用类型后缀(d
在 的情况下double
)或在其后面写一个小数点来完成。后一种方式通常是优选的。在你的情况下:
(int)Math.Ceiling(System.DateTime.DaysInMonth(2009, 1) / 7.0);
Notice that this decimal point notation always yields a double
. To make a float
, you need to use its type suffix: 7f
.
请注意,此小数点表示法始终生成 a double
。要制作float
,您需要使用其类型后缀:7f
。
This behaviour of the fundamental operators is the same for nearly all languages out there, by the way. One notable exception: VB, where the division operator generally yields a Double
. There's a special integer division operator (\
) if that conversion is not desired. Another exception concerns C++ in a weird way: the difference between two pointers of the same type is a ptrdiff_t
. This makes sense but it breaks the schema that an operator always yields the same type as its operands. In particular, subtracting two unsigned int
does notyield a signed int
.
顺便说一句,基本运算符的这种行为对于几乎所有语言都是相同的。一个值得注意的例外:VB,除法运算符通常产生一个Double
. \
如果不需要这种转换,则有一个特殊的整数除法运算符 ( )。另一个异常以一种奇怪的方式与 C++ 有关:相同类型的两个指针之间的区别是ptrdiff_t
. 这是有道理的,但它打破了运算符始终产生与其操作数相同类型的模式。特别是,减去2unsigned int
并没有产生signed int
。
回答by Alex Jordan
As far as I know, you can't force a function to return a different type, so casting the result is your best bet. Casting the result of the function to a double and then dividing should do the trick.
据我所知,你不能强制一个函数返回不同的类型,所以转换结果是你最好的选择。将函数的结果转换为 double 然后除以应该可以解决问题。
回答by casperOne
Change the 7 to a double:
将 7 更改为双精度:
(int) Math.Ceiling(System.DateTime.DaysInMonth(2009, 1) / 7.0);
回答by Igor Zelaya
just divide with a literal double:
只需除以字面双精度:
(int)Math.Ceiling((System.DateTime.DaysInMonth(2009, 1) / 7.0))
回答by Austin Salonen
To expand upon Konrad's answer...
为了扩展康拉德的回答......
Changing 7 to 7.0, 7 to 7D, 7 to 7M all get you the answer you want as well.
将 7 更改为 7.0、7 更改为 7D、7 更改为 7M 也都可以获得您想要的答案。
回答by ctsears
For a completely different approach that avoids casting and floating-point math altogether...
对于完全避免强制转换和浮点数学的完全不同的方法......
int result = val1 / val2;
if (val1 % val2 != 0) result++;
So, in your case...
所以,在你的情况下...
int numDaysInMonth = System.DateTime.DaysInMonth(2009, 1);
int numWeeksInMonth = numDaysInMonth / 7;
if (numDaysInMonth % numWeeksInMonth != 0) numWeeksInMonth++;
This approach is quite verbose, but there might be some special cases where it is preferable. Technically, there should be a slight performance advantage to the modulus approach, but you'll be hard-pressed to measure it.
这种方法非常冗长,但在某些特殊情况下可能更可取。从技术上讲,模数方法应该有轻微的性能优势,但您将很难对其进行测量。
In your case, I'd stick with the accepted answer :-)
在你的情况下,我会坚持接受的答案:-)