Pacobart
Pacobart

Reputation: 261

Divide a whole number by a decimal number

I am trying to divide a whole number by a decimal/float/double. I am able to divide whole numbers just find using: int Num1 = 300 / 2;, but when I try to make that "2" a decimal it won't work. I have seen people mention doing this int Num1 = 300 / ((float)1.25);. That honestly doesn't make any sense to me... I have also tried int Num1 = Decimal.Divide(300, 1.25); without any luck..

Upvotes: 0

Views: 1462

Answers (4)

Martin Mulder
Martin Mulder

Reputation: 12934

Simply put:

  • If you do an arithmatic operation (like +, -, *, /) on two numbers of a different type, the compiler converts the "smallest" type to the "bigest" type which can hold the most information. So, if you are dividing 300 (an int) by 1.25 (a double), the compiler will convert 300 to a double and than devide both doubles. The resulting type will be of the same type, so: a double.
  • If you want to put the result of a "bigger" type into a "smaller" type (a type that can hold less information, like fractions), you HAVE to convert this type into the smaller type by using an explicit cast. So, if you want to put a double into an int, your have to cast it to an int, resulting in possible loss of information.
  • C# knows many suffixes you can use on constant numbers, to explicitly state what type that number is:
    • 10U ==> uint
    • 10L ==> long
    • 10F ==> float
    • 10D ==> double
    • 10M ==> decimal.

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 149108

The problem is that you're trying to save the the result to an int. Try this:

float result = 300 / (float)2;
float result = 300 / (float)1.25;

Or for more brevity (the f is a signal to the compiler that this is a float constant):

float result = 300 / 2f;
float result = 300 / 1.25f;

Note that float is very different from a decimal, and both have their advantages. To use a decimal:

decimal result = decimal.Divide(300, 1.25);

Or this (the m is a signal to the compiler that this is a decimal constant):

decimal result = decimal.Divide(300m, 1.25m);

Upvotes: 4

keyboardP
keyboardP

Reputation: 69392

An int can only store whole numbers. When you divide a whole number by a decimal, the output will be a decimal (even if you use 4.00 because of the way floating points are stored).

This won't compile

int Num1 = 300 / ((float)1.25);

Even though you're casting an explicit float (otherwise 1.25 would be a double), you're trying to store it in a variable of type int. The compiler will not do this automatically because an implicit conversion doesn't exist for it to cast a float as an int for you. Therefore, you need to either tell the compiler that you don't mind losing the precision and cast it as an int

int Num1 = (int)(300 / ((float)1.25));

Or you can change the type of Num1 to be a float.

float Num1 = 300 / ((float)1.25);

Upvotes: 0

Kenneth
Kenneth

Reputation: 28747

You are trying to store the result in an integer. Instead use a double as the type of the variable to store the result:

double Num1 = 300 / 2;
double Num1 = 300 / 1.25;

Upvotes: 1

Related Questions