Quotients to 2 Decimal places - c#

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

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# Keeps Rounding Numbers Up Producing inaccurate results

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;

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

Quotient in Windows phone 7 + silverlight + C#

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

Integer division with decimals

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.

Categories

Resources