floats being saved as 0 after calculation [duplicate] - c#

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Closed 5 years ago.
Very confused here, I have this code:
float temp = (1/10)*100;
Label.Text = Convert.ToString(temp);
for some reason my temp variable is being saved as 0 which means my label text is changed to 0 when I'm expecting 10, I have the same issue when using doubles instead. What has gone wrong?

Since 1, 10 and 100 are all integer values, the division is also an int value, rounded down. In this case 1/10 = 0 so (1/10)*100 = 0
If you do not want this try using floats:
(1.0f/10)*100
In case you're working with integer variables you have to convert them first. This can be achieved by casting, like so:
int a=1;
...
float b = ((float) a)/10; // b will be 0.1
Another quick way of doing this in a line with multiple operations is multiplying by 1.0f:
int x = 100;
float c = (a*1.0f)/x; // c will be 0.01f

Related

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 ;

Converting division of int to int to a decimal is giving 0 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 4 years ago.
I came up with a formula to keep numbers a simple int and was surprised to see that the following was producing a 0.
var x = 45;
var y = 100;
var z = Convert.ToDecimal(x / y * 100);
In the above example, it almost seems ridicilous that I'm dividing by 100, then multiplying by 100 but in most cases, the x and y values are not nice integers.
When my x and y values end up being integers, the above conversion produces a 0. Why is that?
X / Y = 0.45
Int conversion of 0.45 is 0.
Therefore 0 * 100 = 0.
Therefore Convert.ToDecimal(0) is 0.
This does look a bit odd, but the problem there is with the order the expressions are evaluated: first the arguments of Convert are evaluated, and that is done inintegers. That’s why the result is truncated.
You should replace the conversation with an expression that forces evaluation in decimals:
var z = 100M * x / z;

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.

C# - Having trouble dividing by a 0.x number [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
My C# program requires two numerical outputs.
A main price will be divided by 20% and 10%.
For example:
Main price = 200
Discounted price: 20% of 200 = 40. Therefore total price is now 160.
I am having issues converting decimals, int's, etc. Here is what I have tried.
int discountNumber = 20 / 100;
decimal DiscountedPrice = Convert.ToInt32(TotalPrice)
/ Convert.ToInt32(discountNumber);
txtTotalPrice.Text = DiscountedPrice.ToString();
The problem in the
int discountNumber = 20 / 100;
it's always zero
change the int to a double. 20/100 will give you a decimal figure not an int

Division in C# to get exact value [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 9 years ago.
If I divide 150 by 100, I should get 1.5. But I am getting 1.0 when I divided like I did below:
double result = 150 / 100;
Can anyone tell me how to get 1.5?
try:
double result = (double)150/100;
When you are performing the division as before:
double result = 150/100;
The devision is first done as an Int and then it gets cast as a double hence you get 1.0, you need to have a double in the equation for it to divide as a double.
Cast one of the ints to a floating point type. You should look into the difference between decimal and double and decide which you want, but to use double:
double result = (double)150 / 100;
Make the number float
var result = 150/100f
or you can make any of number to float by adding .0:
double result=150.0/100
or
double result=150/100.0
double result = (150.0/100.0)
One or both numbers should be a float/double on the right hand side of =
If you're just using literal values like 150 and 100, C# is going to treat them as integers, and integer math always "rounds down". You can add a flag like "f" for float or "m" for decimal to not get integer math. So for example result = 150m/100m will give you a different answer than result = 150/100.

Categories

Resources