Code:
double PagesCount = 9 / 6 ;
Console.WriteLine(Math.Round(PagesCount, 0));
I'm trying to round the answer to 9/6(1,5) to 2 but this code snippet always results in 1.
How can I avoid that?
9 / 6 is an integer division which returns an integer, which is then cast to a double in your code.
To get double division behavior, try
double PagesCount = 9d / 6 ;
Console.WriteLine(Math.Round(PagesCount, 0));
(BTW I'm picky but decimal in the .net world refers base 10 numbers, unlike the base 2 numbers in your code)
Math.Round is not the problem here.
9.0 / 6.0 ==> 1.5 but 9 / 6 ==> 1 because in the second case an integer division is performed.
In the mixed cases 9.0 / 6 and 9 / 6.0, the int is converted to double and a double division is performed. If the numbers are given as int variables, this means that is enough to convert one of them to double:
(double)i / j ==> 1.5
Note that the integer division is complemented by the modulo operator % which yields the remainder of this division:
9 / 6 ==> 1
9 % 6 ==> 3
because 6 * 1 + 3 ==> 9
Related
This question already has answers here:
Why division is always ZERO? [duplicate]
(3 answers)
Maths is wrong: always result of 0
(5 answers)
Closed 2 years ago.
I'm writting a calculator class that takes a math expression as string, solves it, and returns the result as double.
So far everything works as intended.
My problem is that one of my unit tests fails.
// this test should pass but fails with messag:
// Assert.AreEqual failed. Expected:<0,6000000000000001>. Actual:<1>.
Assert.AreEqual(Solve("1 + 2 - 3 * 4 / 5"), ( 1 + 2 - 3 * 4 / 5));
You can test the problem with this code:
using System;
public class Program
{
public static void Main()
{
double r = 1 + 2 - 3 * 4 / 5; // should be 0.6 or 0.6000000000000001
Console.WriteLine(r == 1); // Prints True
Console.WriteLine("Result: " + r); // Prints 1
}
}
Fiddle https://dotnetfiddle.net/rRZtAu
How do i get the correct mathematical result?
You are getting 1 as a result because you are performing mathematical operations on all integer values.
If you want result in double, then you atleast one value should be double in division
try,
double r = 1 + 2 - 3 * 4 / 5.0;
Step by step execution,
= 1 + 2 - 3 * 4 / 5.0
//^^^^ multiplication will execute first = 12
= 1 + 2 - 12 / 5.0
//^^^^^^^^ this will return 2.4
= 1 + 2 - 2.4
//^^^^^^^ 0.4
= 0.6 > result
If a and b are integers, then a / b is also an integer. If the result is not a whole number, it will be rounded towards zero.
1 + 2 - 3 * 4 / 5
= 1 + 2 - 12 / 5
= 1 + 2 - 2 // <-- 12 / 5 = 2.4, rounded to 2
= 1
It will only be cast to a double once you do the assignment, which is after the value has been computed.
To get the expected result, use a double in your division:
1 + 2 - 3 * 4 / 5.0
= 1 + 2 - 12 / 5.0
= 1 + 2 - 2.4 // <-- 12 / 5.0 = 2.4, not rounded since we're dividing by a double
= 0.6
This question already has answers here:
1/252 = 0 in c#?
(3 answers)
Closed 5 years ago.
Why it returns 0?
print(70 * (50 / 100));
70 * 0,5 = 35
I don't know why this is happening. Or did I make some stupid mistake either ... I don't know
The 50/100 is a division between integers. Since 50<100, the result is 0 and consequently 70*(50/100) results in 0.
If you want to avoid this, you have to cast one of them as a double.
70 * (50 / (double)100)
or
70 * ((double) 50/ 100)
When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2.
So on 50/100 is equal to 0.35 and because you are diving two Integers, it will ignore decimal places so it's gonna be a zero - 0, so computer see it as : (ignoring .35)
70 * 0 = 0
P.S
Maybe you could explore little bit about invoking Decimal.Divide, your int arguments get implicitly converted to Decimals so .35 won't be ignored.
You can also enforce non-integer division on int arguments by explicitly casting at least one of the arguments to a floating-point type, e.g.:
int a = 42;
int b = 23;
double result = (double)a / b;
That's how it comes.. :)
Because / will output int and not double value. The result should be 0.5 and 0 will be taken as int then it will be multiplied by 70 and the result will be 0.
You need to make a cast as follows :
double x = 50/(double)100 ;
Then:
print(70 * x);
50 / 100 is 0 and 70 * 0 is 0.
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 7 years ago.
Let's assume I have 8 items.
From these 8, 5 are success and 3 failure.
If I want to get the success and failure in percentage with 2 decimals precission I will do like this:
int total = 8;
int success = 5;
int failure = 3;
string success =((decimal)((success * 100) / total)).FormatDecimal();
string failure = ((decimal)((failure * 100) / total)).FormatDecimal();
Format decimal is an extension that will convert decimal to string with x amount of decimals.
public static string FormatDecimal(this decimal value, int decimals = 2)
{
return value.ToString(string.Format("0.{0}", new string('0', decimals)));
}
Now if I take my calculator and I do this, the result is correct:
success: (5 * 100) / 8 = 62.5 %
failure: (3 * 100) / 8 = 37.5 %
However my solution return me 62.00 % and 37.00%
What's wrong with my code?
Because your code is running with integer division but you calculator can do floating-point division.
Your (5 * 100) / 8 returns 62, not 62.5 since both operand is int and this operation will always disregards fractional part.
From / Operator (C# Reference)
When you divide two integers, the result is always an integer. For
example, the result of 7 / 3 is 2.
If you change your total to double, you can fix this since you start doing floating-point division not integer division.
double total = 8.0;
Check out;
7.7.2 Division operator
That's because the division operator / for integers only return the integer part.
If need to cast to float, double or decimal.
var result = ((float)(5 * 100)) / 8;
If any of the values you are dividing is a float, double or decimal, the division operator will support the decimal part.
This is very basic mistake at C#. You have defined the calculation wrong.
(success * 100) / total
It means that after success * 100, the result will be parsed as integer. It is now 300 in integer. 300 / 8 = 37 in integer.
Instead, you can replace the 100 with 100m to force convert them to decimal.
I am trying to round numbers to 10
ex:
6 becomes 10
4 becomes 0
11 becomes 10
14 becomes 10
17 becomes 20
How would I do this?
Math.Round doesn't work with this as far as I know.
For double (float and decimal will require additional casting):
value = Math.Round(value / 10) * 10;
For int :
value = (int) (Math.Round(value / 10.0) * 10);
I coded some calculation stuff (I copied below a really simplifed example of what I did) like CASE2 and got bad results. Refactored the code like CASE1 and worked fine. I know there is an implicit cast in CASE 2, but not sure of the full reason. Any one could explain me what´s exactly happening below?
//CASE 1, result 5.5
double auxMedia = (5 + 6);
auxMedia = auxMedia / 2;
//CASE 2, result 5.0
double auxMedia1 = (5 + 6) / 2;
//CASE 3, result 5.5
double auxMedia3 = (5.0 + 6.0) / 2.0;
//CASE 4, result 5.5
double auxMedia4 = (5 + 6) / 2.0;
My guess is that /2 in CASE2 is casting (5 + 6) to int and causing round of division to 5, then casted again to double and converted to 5.0.
CASE3 and CASE 4 also fixes the problem.
5 + 6 is integer 11; which you then cast to double (in the assignment) and divide by two; 5.5
5 + 6 is integer 11; integer 11 / 2 = 5 under integer arithmetic, which you then cast to double (in the assignment)
5.0 + 6.0 is double 11.0; divide by double 2.0 giving double 5.5
5 + 6 is integer 11; there is an implicit cast to double 11.0 for the division, then divide double 2.0 giving double 5.5
To expand on Marc's (correct) answer a bit, whole numbers are interpreted as integer, whereas numbers with decimal points are interpreted as double. To declare a whole number as a literal double, append a "D" to it:
//CASE 2b, result 5.5
double auxMedia2b = (5D + 6D) / 2;
You are correct. CASE 2 uses integer arithmetic until the assignment is made. You can also fix the problem by making an explicit cast:
double auxMedia1 = ((double) (5 + 6)) / 2;
//CASE 2, result 5.0
double auxMedia1 = (5 + 6) / 2;
The result of the (5 + 6) operation is integer. Because both operands are of type integer. Then, the compiler performs 11 / 2, where both operand are also integers. The result of the last division is obviously 5, because it is an integer division (don't know the proper English word).