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

c#
13,024

Solution 1

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.

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.

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:

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

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 not yield a signed int.

Solution 2

Change the 7 to a double:

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

Solution 3

just divide with a literal double:

(int)Math.Ceiling((System.DateTime.DaysInMonth(2009, 1) / 7.0))

Solution 4

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.

Solution 5

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.

Share:
13,024

Related videos on Youtube

Element
Author by

Element

Updated on April 24, 2022

Comments

  • Element
    Element about 2 years

    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.

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

  • Element
    Element about 15 years
    Ah, thank you, this was confusing me because I come from vb background and the same line of code was working as expected in vb.net but not c#. I changed the parenthesis to cast the first operand and it worked
  • Igor Zelaya
    Igor Zelaya about 15 years
    you don't need the double casting anymore
  • Tim Frey
    Tim Frey about 15 years
    Um...what? I think the question is how to use floating-point division rather than integer division.
  • Daniel Schaffer
    Daniel Schaffer about 15 years
    wouldn't using 7d instead of using explicit casting work the same way be less ugly?
  • Konrad Rudolph
    Konrad Rudolph about 15 years
    @Daniel, sixlettervariables: You're of course right, I lapsed (although I would never use the suffix D for doubles, since this is redundant: rather, I'd recommend writing 7.0). However, others have already answered this. I guess we can coexist.
  • Sam
    Sam about 15 years
    Hah, didn't realize I put a 'd' there, so I'm with you '7.0'!
  • Konrad Rudolph
    Konrad Rudolph about 15 years
    Well guys, you've got your wish. I completely rewrote the entry. I also made it a community wiki.
  • Alex Jordan
    Alex Jordan about 15 years
    In C#, dividing an int by an int always returns an int. To use fp division, you have to divide doubles. Since you can't make the function return a double, you have to cast either the function's return value to a double, or make the divisor a double.