Math.Round rounds me - c#

I understand if .NET rounds 2.5 to 2 using banker's rounding. But, how this could be:
decimal point;
point =51 * 70 / 100;
Math.Round(point,0, MidPointRounding.AwayFromZero);
rounds to 35?
How can I make all .5's round to upper integer even if it's odd?

This second line in your snippet already gives you an integer result.
51, 70, 100 are of type int, therefore the operators for integer multiplication and division are chosen. The result of an integer multiplication or division is always of type integer again, and possible decimal places are truncated when dividing using / on integers.
The statement point = 51 * 70 / 100; is equivalent to
int tmp = 51 * 70; // result is 3570
tmp = tmp / 100; // result is 35 (!!!)
point = (decimal)tmp; // point is 35m;
The solution is to change your code so that it uses decimal arithmetic:
point = 51m * 70m / 100m; // point is 35.7m
Actually it is sufficient that one of the operands is of type decimal. This can either be achieved by using the suffix m (for monetary) or by using a type cast. The following sample will also give the desired result:
point = (decimal)51 * 70 / 100;

You are doing integer division. Try this instead:
decimal point = 51m * 70m / 100m;

Related

changing celsius to fahrenheit and vice versa in c# [duplicate]

This simple calculation is returning zero, I can't figure it out:
decimal share = (18 / 58) * 100;
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
18 / 58 is an integer division, which results in 0.
If you want decimal division, you need to use decimal literals:
decimal share = (18m / 58m) * 100m;
Since some people are linking to this from pretty much any thread where the calculation result is a 0, I am adding this as a solution as not all the other answers apply to case scenarios.
The concept of needing to do calculations on various types in order to obtain that type as a result applies, however above only shows 'decimal' and uses it's short form such as 18m as one of the variables to be calculated.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Because the numbers are integers and you perform integer division.
18 / 58 is 0 in integer division.
Whenever I encounter such situations, I just upcast the numerator.
double x = 12.0 / 23409;
decimal y = 12m / 24309;
Console.WriteLine($"x = {x} y = {y}");
double res= (firstIntVar * 100f / secondIntVar) / 100f;
when dividing numbers I use double or decimal , else I am getting 0 , with this code even if firstIntVar && secondIntVar are int it will return the expected answer
decimal share = (18 * 100)/58;
Solved: working perfectly with me
int a = 375;
int b = 699;
decimal ab = (decimal)a / b * 100;

Lamda expression not working on String.Count() [duplicate]

This simple calculation is returning zero, I can't figure it out:
decimal share = (18 / 58) * 100;
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
18 / 58 is an integer division, which results in 0.
If you want decimal division, you need to use decimal literals:
decimal share = (18m / 58m) * 100m;
Since some people are linking to this from pretty much any thread where the calculation result is a 0, I am adding this as a solution as not all the other answers apply to case scenarios.
The concept of needing to do calculations on various types in order to obtain that type as a result applies, however above only shows 'decimal' and uses it's short form such as 18m as one of the variables to be calculated.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Because the numbers are integers and you perform integer division.
18 / 58 is 0 in integer division.
Whenever I encounter such situations, I just upcast the numerator.
double x = 12.0 / 23409;
decimal y = 12m / 24309;
Console.WriteLine($"x = {x} y = {y}");
double res= (firstIntVar * 100f / secondIntVar) / 100f;
when dividing numbers I use double or decimal , else I am getting 0 , with this code even if firstIntVar && secondIntVar are int it will return the expected answer
decimal share = (18 * 100)/58;
Solved: working perfectly with me
int a = 375;
int b = 699;
decimal ab = (decimal)a / b * 100;

basic maths calculations in c# [duplicate]

This simple calculation is returning zero, I can't figure it out:
decimal share = (18 / 58) * 100;
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
18 / 58 is an integer division, which results in 0.
If you want decimal division, you need to use decimal literals:
decimal share = (18m / 58m) * 100m;
Since some people are linking to this from pretty much any thread where the calculation result is a 0, I am adding this as a solution as not all the other answers apply to case scenarios.
The concept of needing to do calculations on various types in order to obtain that type as a result applies, however above only shows 'decimal' and uses it's short form such as 18m as one of the variables to be calculated.
// declare and define initial variables.
int x = 0;
int y = 100;
// set the value of 'x'
x = 44;
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0.
Console.WriteLine( (x / y).ToString() );
// Results in 0 as the whole number 44 over the whole number 100 is a
// fraction less than 1, and thus is 0. The conversion to double happens
// after the calculation has been completed, so technically this results
// in 0.0
Console.WriteLine( ((double)(x / y)).ToString() );
// Results in 0.44 as the variables are cast prior to calculating
// into double which allows for fractions less than 1.
Console.WriteLine( ((double)x / (double)y).ToString() );
Because the numbers are integers and you perform integer division.
18 / 58 is 0 in integer division.
Whenever I encounter such situations, I just upcast the numerator.
double x = 12.0 / 23409;
decimal y = 12m / 24309;
Console.WriteLine($"x = {x} y = {y}");
double res= (firstIntVar * 100f / secondIntVar) / 100f;
when dividing numbers I use double or decimal , else I am getting 0 , with this code even if firstIntVar && secondIntVar are int it will return the expected answer
decimal share = (18 * 100)/58;
Solved: working perfectly with me
int a = 375;
int b = 699;
decimal ab = (decimal)a / b * 100;

C# not calculating a decimal result properly [duplicate]

This question already has answers here:
Extremely basic division equation not working in c#
(3 answers)
Closed 9 years ago.
(5 / 15) * 1185 should give 395.
decimal Test = (5 / 15) * 1185;
This, however, returns 0. What am I doing wrong?
5 / 15 is an integer division and returns 0. Use 5m / 15m to force decimal.
m is the C# suffix to signify to the compiler that the number your wrote is a decimal, even if it looks like an int. You can use f for floats and d for doubles (or 5.0).
While on the topic of suffixes, there is also L for long but that wouldn't have helped your with your division because it is also an integer type.
5 // Int32
5L // Int64
5d // Double
5.0 // Double
5m // Decimal
5f // Single
You need to use floating point division, not integer division. You can get the correct result by making it a floating point number, or using m (to specify it as a decimal) behind the 15:
decimal Test = (5 / 15.0) * 1185;
or
decimal Test = (5 / 15m) * 1185;
You could also use a lambda expression :
Func<decimal, decimal, decimal, decimal> exp = (x, y, z) => (x / y) * z;
Console.WriteLine(exp(5, 15, 1185));
outputs:
394.999999
as you are expecting 395 you will need to use a Math.Round
Console.WriteLine(Math.Round(exp(5, 15, 1185), 1));
outputs: 395.0
5/15 is 0 because you are using integer division.
You can try use one of the following code:
decimal test = (5m / 15) * 1185
decimal test = ((decimal)5 / 15) * 1185
decimal test = (5.0 / 15) * 1185
The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal type does not eliminate the need for rounding but rather minimizes errors due to rounding.
Link: http://msdn.microsoft.com/en-us/library/364x0z75

Division with float did not give expected result in C#

I am facing a problem in dividing numbers in c#.
See my code in C# for division
double openRate = 0,
long a=542;
long b=4795;
openRate =(a/b)*100
This gives 11.303.. in my calculator .
But c# gives me 0.0
What could be reason?
When you write
long a = 542;
long b = 4795;
Since because a / b is calculated as an integral value; any fractional part was dropped. So a / b is equal 0 at this point not 0,113...
From elemantary school math;
0 * 100 = 0
Your calculator use probably floating division so actually it calculates this like;
double openRate = 0;
long a = 542;
long b = 4795;
openRate =((double)a / b) * 100; // 11.303...
a and b are integers and get divided using the operator/ of long, resulting in an integral division.
542 / 4795 = 0.113
After this they got multiplied with 100, which is an integer, either.
0 * 100 = 0
Last but not least the (still integral) result get's converted into an double. What you want to write is something like this:
openRate = ((double)a / (double)b) * 100.0;

Categories