This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
What values integer gets? When i wrote this code the output was 0 and not 0.333333
int a=18;
Int b=6;
int c=b/a;
Console.WriteLine(c);
thank you (:
int can not show Decimal. you must use float or double
The answer is in your question, integer can only hold integer value (...,-2,-1,0,1,2,...) so 6/18 would be 0. You could expect otherwise if 2 of these conditions are met:
1.c (the variable result is assigned) can hold floating type (float, double, decimal)
either an implicit or an explicit cast is done.
To describe (2) a bit more:
if either of a or b is of floating point type, you implicitly casting the result to a floating point value.
double a = 6;
int b = 18;
double c =a / b;
or you explicitly say that the int values (at leat one of them) should be considered as floating point:
int a = 6;
int b= 18;
double c = (double)a/(double)b;
double c = (double) a/b;
All you need to know about types
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/types
Related
This question already has answers here:
Converting a double to an int in C#
(5 answers)
Closed 6 years ago.
I can't understand the difference between Convert.ToInt32 and Parsing (int) command when i convert a double number to a int number.My example code is here and i have two different answer when i show it.
class Program
{
static void Main(string[] args)
{
double i = 3.897456465;
int y;
y = Convert.ToInt32(i);
Console.WriteLine(y);
y = (int)i;
Console.WriteLine(y);
Console.ReadKey();
}
}
(int) i; is casting not parsing.
Convert.ToInt32(double) would round the number to nearest 32 bit integer. Whereas casting (int) i; would only take the integer part of the double value and assign it to the variable.
Since one does rounding and the other just take the integer value, you see the difference.
Consider the following example:
double d = 1.99d;
int castedValue = (int) d; //1
int convertedValue = Convert.ToInt32(d); //2
In the above code, casting returned 1 since that is the integer part of the double value, whereas the conversion using Convert.ToInt32 rounded the value.
From the documentation from Convert.ToInt32(double):
Return Value
Type: System.Int32
value, rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.
From the C# 5 specification section 6.2.1, explicit numeric conversions:
For a conversion from float or double to an integral type [...]
[...]
Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.
(Emphasis mine.)
So basically, Convert.ToInt32 rounds up or down to the nearest int. Casting always rounds towards zero.
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
I have tried this with decimal, double and float variables. I am dividing an integer by another integer and storing the result in a variable. None of the three data types will store decimal points, for example:
double d;
uint num1 = 20
uint num2 = 3
d = num1 / num2;
//d = 6.0
It is as if it is rounding to the nearest integer, help please?
You should cast either num1 or num2 as a decimal/double/float first before doing the division and storing the result..
When you do math with integers, the result is an integer. That's just how the operators are defined. To do double math, make num1, num2, or both doubles, or cast one of them to a double before calculating.
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 9 years ago.
Today i come with a problem and not able to figure out what is the issue with this simple statement
I Tried
double d =1/4;
expected ans for me is 0.25 but in reality ans is 0.0 why so ??
And what should we do if statement is in terms of integer variables like this
double a =(a-b)/(d+e);
Because what you done is here integer division. 1 / 4 always give you 0 as a result regardless which type you assing it.
.NET has 3 type of division. From 7.7.2 Division operator
Integer division
Floating-point division
Decimal division
From Integer division part;
The division rounds the result towards zero, and the absolute value of
the result is the largest possible integer that is less than the
absolute value of the quotient of the two operands.
If you want to 0.25 as a result, you should define one of your values as a floating point.
You can use one of these;
double d = 1d / 4d;
double d = 1d / 4;
double d = 1 / 4d;
And what should we do if statement is in terms of integer variables
like this
double a =(a-b)/(d+e);
I assume your a, b, d and e are integers, you should use one of these then;
double a = (double)(a-b) / (double)(d+e);
double a = (a-b) / (double)(d+e);
double a = (double)(a-b) / (d+e);
double d =1d/4;
should work.
If you don't specify the type of your numbers, it is treated as Integer. And integer 1/4 will be zero.
Use this:
double d = (double) 1 / 4;
/ Operator (msdn)
When you divide two integers, the result is always an integer. For
example, the result of 7 / 3 is 2. To determine the remainder of 7 /
3, use the remainder operator (%). To obtain a quotient as a rational
number or fraction, give the dividend or divisor type float or type
double. You can assign the type implicitly if you express the dividend
or divisor as a decimal by putting a digit to the right side of the
decimal point.
Try this:
double d = 1.0 / 4.0;
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 9 years ago.
If I divide 150 by 100, I should get 1.5. But I am getting 1.0 when I divided like I did below:
double result = 150 / 100;
Can anyone tell me how to get 1.5?
try:
double result = (double)150/100;
When you are performing the division as before:
double result = 150/100;
The devision is first done as an Int and then it gets cast as a double hence you get 1.0, you need to have a double in the equation for it to divide as a double.
Cast one of the ints to a floating point type. You should look into the difference between decimal and double and decide which you want, but to use double:
double result = (double)150 / 100;
Make the number float
var result = 150/100f
or you can make any of number to float by adding .0:
double result=150.0/100
or
double result=150/100.0
double result = (150.0/100.0)
One or both numbers should be a float/double on the right hand side of =
If you're just using literal values like 150 and 100, C# is going to treat them as integers, and integer math always "rounds down". You can add a flag like "f" for float or "m" for decimal to not get integer math. So for example result = 150m/100m will give you a different answer than result = 150/100.
int x = 73;
int y = 100;
double pct = x/y;
Why do I see 0 instead of .73?
Because the division is done with integers then converted to a double. Try this instead:
double pct = (double)x / (double)y;
It does the same in all C-like languages. If you divide two integers, the result is an integer. 0.73 is not an integer.
The common work-around is to multiply one of the two numbers by 1.0 to make it a floating point type, or just cast it.
because the operation is still on int type. Try double pct = (double)x / (double)y;
Integer division drops the fractional portion of the result. See: http://mathworld.wolfram.com/IntegerDivision.html
It's important to understand the flow of execution in a line of code. You're correct to assume that setting the right side of the equation equal to double (on the left side) will implicitly convert the solution as a double. However, the flow execution dicates that x/y is evaluated by itself before you even get to the double pct = portion of the code. Thus, since two ints are divided by each other, they will evaluate to an int solution (in this case, rounding towards zero) before being implicitly converted to a double.
As other have noted, you'll need to cast the int variables as doubles so the solution comes out as a double and not as an int.
That’s because the type of the left hand operand of the division (x) is of type int, so the return type of x / y is still int. The fact that the destination variable is of type double doesn’t affect the operation.
To get the intended result, you first have to cast (convert) x to double, as in:
double pct = (double)x / y;