Why division is always ZERO? [duplicate] - c#

This question already has answers here:
C# is rounding down divisions by itself
(10 answers)
Closed 6 years ago.
int value = 10 * (50 / 100);
The expected answer is 5, but it is always zero. Could anyone please give me detail explanation why it is?
Thanks much in advance.

Because the result of 50/100 is 0 .
50/100 is equals to int(50/100) which returns 0.
Also, if you want to return 5, use this:
int value = (int)(10 * (50 / 100.0));
The result of (50/100.0) is 0.5.

Because you're doing an integer division: (50 / 100) gives 0
Try this:
int value = (int)(10 * (50 / 100.0));
Or reverse the multiply/division
int value = (10 * 50) / 100;
So it's getting multiplied before the divide

You make operation on int values.
50/100 in int is 0.

Related

Double multiplication with brackets problem [duplicate]

This question already has answers here:
1/252 = 0 in c#?
(3 answers)
Closed last year.
double celsius_0 = (100.5 - 32) * (5 / 9);
double celsius_1 = (100.5- 32) * 5 / 9;
Console.WriteLine(celsius_0);
Console.WriteLine(celsius_1);
output:
0
38,0555555555556
Why do they return differnt values?
I dont understand whats happening
It's because here celsius_0 = (100.5 - 32) * (5 / 9)
you count value in the first bracket then in the second one and then you multiply them.
While in the second line (100.5- 32) * 5 / 9 you count value in bracket then multiply it by 5 and at the end divide it by 9
To sum up in mathematics first you do things in brackets and then multiply/divide and finally add/substract

Multiplying large values results to negative value [duplicate]

This question already has answers here:
Multiplying two positive Int32 returns incorrect, negative answer?
(3 answers)
Closed 3 years ago.
double result = 0.0;
result = 7777 * 7777 * 7777 *7777 ;
Instead of getting the result 3658039542829441, I am getting -1283042943. Can someone suggest how to avoid this? I understand the calculation is still under double type's range.
Each 7777 is an integer literal. So it is performing integer multiplication. You will need to make these double literals using the d suffix. Then it will perform double multiplication.
double result = 0.0;
result = 7777d * 7777d * 7777d * 7777d ;

Why print(70 * (50 / 100)) returns 0? [duplicate]

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.

failed/success percentage from total items [duplicate]

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.

ROUNDUP(700.25;-1) returns 710, how to we do this in C# [duplicate]

This question already has answers here:
Rounding integers to nearest multiple of 10 [duplicate]
(6 answers)
Closed 9 years ago.
I am building an Application based on a excel sheet, at a certain point the sheet uses
=ROUNDUP(701.25;-1) which is returned as 710... how do we do this in C#
I tried Math.Round that returns 701, If I try to use Math.Round() with -1 then i get this
Rounding digits must be between 0 and 15, inclusive.
Please help me out here.
Try this (you'll need to cast the return value to int if necessary):
public static double RoundUp(double value, int digits)
{
double pow = Math.Pow(10, digits);
return Math.Ceiling(value * pow) / pow;
}
This should give you the functionality defined here:
http://office.microsoft.com/en-gb/excel-help/roundup-HP005209242.aspx
use the below two methods.Which may help you
int RoundUp(int toRound)
{
return (10 - toRound % 10) + toRound;
}
int RoundDown(int toRound)
{
return toRound - toRound % 10;
}
Referenece
Rounding integers to nearest multiple of 10

Categories

Resources