What is the best practice to make division return double in C# -


When you want to break the result of a method below in C #, it is best to force it to return What's the way a double value instead of the default integer

  (int) math Siling ((Double) (System.DateTime.DaysInMonth (2009, 1) / 7));  

As you can see, I need a split to double return, so I can use the roof function.

two int one division of numbers int Returns, decreases any decimal point, it is generally true for other data types: arithmetic operations do not change their operating type.

To implement a certain return type, you have to convert the operations appropriately. In your special case, there is really enough to convert an operator to double : In this way, C # will automatically display the conversion for other operating.

You've got options: You can clearly convert operands either. However, the second operand is a literal , it is better to just correct the correct word.

This is either a type of suffix (in the case of D in the double ) or a decimal point behind it The latter method is generally preferred in your case:

  Math.Ciling (System.DateTime.DaysInMonth (2009, 1) / 7.0);  

Note that this decimal point notation always generates an double to create float You will need to use a prefix: 7f .

This behavior of fundamental operators is similar to almost all languages, there is a notable exception from the way: VB, where division operator usually generates double is a special integer division operator ( \ ), if that conversion is not desired. Another exception to C ++ is the odd one: the difference between two points of the same type is a ptrdiff_t . It makes sense, but it breaks the schema that the operator always produces the same type of operand. Specifically, subtracting two unsigned int to not generate a signed int .


Comments