Is there any inbuilt function to get quotient of a division. Math.DivRem is not appplicable it seems.
Division of integer operands will yield the integer quotient. If your inputs are floating point types, just first convert them to integer types:
double num1 = 27;
int num2 = 7;
double dblResult = num1 / num2; // will yield floating point result 3.857...
int intResult =(int)num1 / num2; // will yield integer quotient 3
Related
I can't show the double values in C#
Math.DivRem(double money, int number, out int remain);
Math.DivRem Method is only compatible with Int64 and Int32, which means you can't use double, float or any numeric data type with decimals.
Depending on what you are looking for, this is two other methods of getting the quotient and remainder of your doubles:
double a = 20.1;
double b = 4.93;
double remainder = a % b; // Returns 0.380...
double quotient = a / b; // Returns 4,077...
If you want to make your remainder or quotient to an int, just use
int result = Convert.ToInt32(quotient); // Returns 4
Or to make it more efficient
int result = ConvertToInt32(a / b); // Returns 4
EDIT
One workaround to use Math.DivRem with "doubles", are by multiplying both sides with a constant, for example 100 000. After getting the remainder, divide it with the constant.
double money = 5.515;
int number = 2;
int constant = 100000;
Math.DivRem(Convert.ToInt32(money * constant), number * constant, out int remain);
// Divides 551500 with 200000
// remain = 151500
// For double
double d = (double) remain / (double) constant; // Return 1,515
// For int
remain /= constant; // Return 1
DOUBLE EDIT
Use this instead of Math.DivRem() when dealing with doubles and ints combined.
double money = 12.15;
int number = 5;
double remainder = money % number; // Return 2,15...
int remainder = (int)(money % number); // Return 2
Is there a mod operator in C# for RSA algorithm? I've been using % as I thought this could be used as mod, but the answers I get for for c and m are not correct, so I've realised % doesn't work.
double e = 13;
double d; //decryption
double de = 7;
d = ((de * euiler) + 1) / e;
double message = 25;
double c = Pow(message, e) % n;
double m = Pow(c, d) % n;
The confusion lies in double Type.
MSDN:
The modulus operator (%) computes the remainder after dividing its first operand by its second. All numeric types have predefined modulus
operators.
Note the round-off errors associated with the double type.
the % is a remainder. You might want to make a static function that uses the % to make a modulo operation.
So I need to get a quotient to 2 decimal places like 0.33, but do I need to use doubles all the way down or can I use integers for everything except the quotient and then just use double or decimal for the quotient? It is also breaking because of the quotient. If anyone could help me I would be extremely grateful :)
int firstnumber;
int secondnumber;
decimal quotient;
firstnumber = int.Parse(inputTextBox1.Text);
secondnumber = int.Parse(inputTextBox2.Text);
sumLabel.Text = (firstnumber + secondnumber).ToString();
differenceLabel.Text = (firstnumber - secondnumber).ToString();
productLabel.Text = (firstnumber * secondnumber).ToString();
quotient = decimal.Parse(quotientLabel.Text);
quotient = (firstnumber / secondnumber).tostring;
You can cast the integers to decimal or double when assigning your variable, then assign the label from the results:
quotient = ((decimal)firstnumber / secondnumber);
quotientLabel.Text = quotient.ToString("N2");
Ok so this is doing my head in, it keeps producing a slightly inaccurate result by rounding up the digits after the decimal. i need the exact value, not a rounded one!
So to start with take the following code:
int num1 = 10087;
int num2 = 9971;
int num3 = 9909;
int num4 = 9917;
int num5 = 9904;
double average = (num1 + num2 + num3 + num4 + num5) / 5;
double percentage = (10000 - average) / 100;
If this math is done on a calculator, the value of "percentage" is 0.424. But if it is run through the code the value gets rounded to 0.43 which is inaccurate. How can i stop this happening?
note: please do not question the 10000 number, i also need the result to be exactly the correct number (0.424) that is very important in this case!
cast your average calc to double first
int num1 = 10087;
int num2 = 9971;
int num3 = 9909;
int num4 = 9917;
int num5 = 9904;
double average = (double)(num1 + num2 + num3 + num4 + num5) / 5;
double percentage = (10000 - average) / 100;
Just add a "d" after the numbers...
int num1 = 10087;
int num2 = 9971;
int num3 = 9909;
int num4 = 9917;
int num5 = 9904;
double average = (num1 + num2 + num3 + num4 + num5) / 5d;
double percentage = (10000 - average) / 100d;
The d tells the compiler to make these numbers double precision floating point values instead of integers (you can also just add a decimal point). Without the "d" the numbers are integers and the computer performs integer arithmetic. This means that
9 / 5 = 1 instead of 1.8
Kindly read the msdn form it will help you to know the real reason
reference :- http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx
In it they already say that
When you divide two integers, the result is always an integer
Here you divide two integer so it's result is also int.
You just need to type cast that's all as the other people give ans
As Ela Write your code will look like
int num1 = 10087;
int num2 = 9971;
int num3 = 9909;
int num4 = 9917;
int num5 = 9904;
double average = (double)(num1 + num2 + num3 + num4 + num5) / 5;
double percentage = (10000 - average) / 100;
What's the proper way to do "integer" divisions with decimal types in C# ?
I.e.
decimal a = 130, b = 60;
decimal res = a / b; //need to get 2.0, not 2.6666
In this case I'd use the Floor function.
decimal res = Math.Floor(a / b);
decimal a = 130, b = 60;
decimal res = Math.Floor(a/b);
You can use Decimal.Truncate(a / b);
Decimal.Truncate() "rounds" towards zero, and is thus like Math.Floor() for positive numbers and Math.Ceiling() for negative numbers.