This question already has answers here:
What is the best practice to make division return double in C#
(6 answers)
Closed 8 years ago.
I'm trying to make a double divion with this code:
int a = 5;
int b = 5;
double result = (a + b) / 4;
How to get a 2.5 as a result and not 2, please?
Thanks in advance!
One of the arguments needs to be a double. So:
double result = (a + b) / 4.0;
Or
double result = (double)(a + b) / 4;
Related
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Division returns zero
(8 answers)
Closed 2 years ago.
I was doing a data science pet project, this is a simplified version of the problem:
int b = 102;
int c = 248;
double a = (b / c) * 100;
Console.WriteLine(a); // prints 0
This code prints zero for some reason. Any other alternatives? Why is this happening?
Because "b / c" is zero, you should use:
int b = 102;
int c = 248;
double a = ((double)b / c) * 100;
int k = Convert.ToInt32(a);
Console.WriteLine(k);
This question already has answers here:
Is floating point math broken?
(31 answers)
Modulus gives wrong outcome?
(1 answer)
Closed 4 years ago.
float a = 0.95f;
float b = 5.05f;
float ab = (a + b);
bool isTrue = (ab == 6.0f);
bool isFalse = ((a + b) == 6.0f);
bool isTrueAgain = ((float)(a + b) == 6.0f);
Why isTrue is true?
Why isFalse is false?
This question already has answers here:
How to round up value C# to the nearest integer?
(10 answers)
Closed 6 years ago.
int a = 4;
int b = 3;
int c = a/b;
Here the c value as 1.33 . now i need to change the value as whole number like 2.
If,
int a =1;
int b = 2;
int c = a/b means the value is 0.5
now i need to change the value as next whole number like 1
Please try this:
int a = 4;
int b = 3;
int c = Convert.ToInt32(Math.Ceiling(a/b));
This question already has answers here:
C# '+=' Operator
(2 answers)
Closed 6 years ago.
I don't really get what the function of "+=" is.. Could someone explain it to me? Like what's the different between "=" and "+="
I've got something like this now:
b +=(a[i]);
Thanks!
b = a assigns a to b
b += a assigns b+a to b
Ex:
int a = 5;
int b = 2;
b = a; //b is now 5
vs.
int a = 5;
int b = 2;
b += a; //b is now 7
b +=(a[i]);
It is a shortcut for this
b = b + a[i];
https://msdn.microsoft.com/en-us/library/sa7629ew.aspx
b = a[i] + b
But you can use it for add event
Button.Click += new System.EventHandler(Button_Click);
How does C# execute this?
static void Main(string[] args)
{
int i = 4;
i *= 4 + 8 / 2;
Console.WriteLine(i);
}
This was asked in one of the interview Questions. And I applied BODMAS to it.
But it was wrong. Please Explain.
It will be executed equivalent to the following code:
int i = 4;
int temp = 8 / 2;
temp = 4 + temp;
i = i * temp;
The compiler will shorten it down because it can calculate the constant that is on the right of i *=, so in reality it will compile to this:
int i = 4;
i *= 8;
i *= 4 + 8 / 2 is executed as:
i = i * (4 + (8 / 2))
That's the correct way of reading it.
Operator precedence is very clear on this: The / is a multiplicative operator and is applied first, then the +. The *= is an assignment operator and is applied last.
So:
8 / 2 = 4
4 + 4 = 8
i *= 8;
so i will be 4 * 8 = 32;