Integer division with decimals - c#

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.

Related

How can I have a double value using Math.DivRem()?

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

c# Pow/Datatype Conversion

I'm fairly new to programming in general. I have read through a raft of posts regarding casting datatype when using the POW function and have tried to take note.
I can't understand why the following isn't working. In the debugger y is getting assigned to 0. I can't understand why this is. I think it is something to do with how I am converting Doubles/Decimals?
public decimal MonthsPayment(){
decimal monthlyint = this.Rate / 12;
int totalpayments = this.Term * 12;
decimal x = monthlyint * this.Amount;
decimal s1 = (1+monthlyint);
decimal s2 = (-totalpayments);
decimal y = (decimal)(Math.Pow((double)s1,(double)s2));
decimal Repayment = x /(1 - y);
return Repayment;

Quotients to 2 Decimal places

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");

Round to 1 decimal place in C#

I would like to round my answer 1 decimal place. for example: 6.7, 7.3, etc.
But when I use Math.round, the answer always come up with no decimal places. For example: 6, 7
Here is the code that I used:
int [] nbOfNumber = new int[ratingListBox.Items.Count];
int sumInt = 0;
double averagesDoubles;
for (int g = 0; g < nbOfNumber.Length; g++)
{
nbOfNumber[g] = int.Parse(ratingListBox.Items[g].Text);
}
for (int h = 0; h < nbOfNumber.Length; h++)
{
sumInt += nbOfNumber[h];
}
averagesDoubles = (sumInt / ratingListBox.Items.Count);
averagesDoubles = Math.Round(averagesDoubles, 2);
averageRatingTextBox.Text = averagesDoubles.ToString();
You're dividing by an int, it wil give an int as result. (which makes 13 / 7 = 1)
Try casting it to a floating point first:
averagesDoubles = (sumInt / (double)ratingListBox.Items.Count);
The averagesDoubles = Math.Round(averagesDoubles, 2); is reponsible for rounding the double value. It will round, 5.976 to 5.98, but this doesn't affect the presentation of the value.
The ToString() is responsible for the presentation of decimals.
Try :
averagesDoubles.ToString("0.0");
Do verify that averagesDoubles is either double or decimal as per the definition of Math.Round and combine these two lines :
averagesDoubles = (sumInt / ratingListBox.Items.Count);
averagesDoubles = Math.Round(averagesDoubles, 2);
TO :
averagesDoubles = Math.Round((sumInt / ratingListBox.Items.Count),2);
2 in the above case represents the number of decimals you want to round upto. Check the link above for more reference.
int division will always ignore fraction
(sumInt / ratingListBox.Items.Count);
here sumint is int and ratingListBox.Items.Count is also int , so divison never results in fraction
to get the value in fraction , you need to datatype like float and type cast the sumInt and count to float and double and then use divison
var val= Math.Ceiling(100.10m);
result 101

Twos Complement On A Double

How do I perform a twos complement on a double and return a double?
If you are trying to do the two's complement of the internal bit representation of the double, you can use the BitConverter class.
Something like:
double x = 12345.6;
Int64 bits = BitConverter.DoubleToInt64Bits(x);
bits = ~bits + 1;
x = BitConverter.Int64BitsToDouble(bits);
I'm not sure why you would want to do this, though...
You might need to cast to a long and then do the twos complement and cast back:
double x = 1245.1;
long l = (long)x;
l=~l; l++; /* complement followed by + 1 */
x = (double)l;
I didn't test this, but hopefully it gets you on the right track.
Edit: Since you cannot cast from double to long with bit representation then you might need to do something like:
double x = 1234.5;
ulong l;
unsigned char * d = (unsigned char *) &x;
l = (ulong)(*d);
l=~l; l++;
d = (unsigned char *) &l;
x = (double)(*d);
Again untested...

Categories

Resources