C# Two decimal places [duplicate] - c#

This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Why does Decimal.Divide(int, int) work, but not (int / int)?
(8 answers)
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 1 year ago.
How do I insert decimal point into string?
int attrval = Convert.ToInt32("000504");
decimal val1 = (attrval / 100);
string val2 = String.Format("{0:.00}", attrval / 100);
val1 = 5 need 5.04
val2 = 5.00 need 5.04

You have an issue with integer division in the line
decimal val1 = (attrval / 100);
Since both attrval and 100 are of type int the result is int as well: 5. This line should be
decimal val1 = (attrval / 100m);
please, note m suffix: here we divide int by decimal (100m) and have a desired decimal result. Same for val2:
string val2 = String.Format("{0:.00}", attrval / 100m);

Division of two integer numbers return integer value as a result. This division rounds resultant value towards zero, that is why you are getting val2 = 5 instead of 5.04.
If you want result to be in decimal type, convert at-least one value either numerator or denominator to the decimal.
decimal val1 = (attrval / (decimal)100); //5.04
or
decimal val1 = (attrval / 100m); //5.04
Now convert it into string,
string val2 = String.Format("{0:.00}", var1); //"5.04"
Try it online

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

Dividing results in zero for some reason? [duplicate]

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

Change Integer value to Roundoff by the nearest integer [duplicate]

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

c# Math function not calulating

I have a simple math function
private int Doyle(LogArguments args)
{
return Convert.ToInt32(Math.Round(Math.Pow((args.Diameter - 4) , 2.0) * (args.Length / 16),0));
}
that should always return an int > 0 but my problem is if args.Length <= 16 it args.Length/16 = 0.
I have tried:
decimal d = args.length/16
and double d = args.length/16;
which both turn out to 0 when Length <16
If args.length is an int you are doing integer division regardless of what the type of the output variable is. To force decimal division the simplest way is to specify a decimal literal in the denominator:
decimal d = args.length / 16M;
Or for a double use a floating-point literal:
double d = args.length / 16.0;
double d = args.length/16;
which both turn out to 0 when Length <16
That is because args.Length and 16 are both integers. You get integer division (result 0), which is then cast to a double. Instead, try
double d = args.length/16.0;
If you are trying to multiply by a float value such as this:
5 / 16 = 0.3125
Then you should turn (args.Length / 16) into (args.Length / 16f).
This will force the division to be does as a floating point, and keep any remainder. Without the f added, it will do it as an integer division which truncates the remainder, so any value less than 16 will be less than 1 and get reduced to 0.

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

Categories

Resources