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

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

Related

C#: How to round a float to a defined amount of relevant digits? [duplicate]

This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
How can I round a Decimal value upto a precision so that the total number of digits doesnot cross 15?
(2 answers)
Closed 8 months ago.
How can I limit a float to a defined amount of digits? Let's say I want a precision of 4, then it should be:
14.49193 -> 14.49
1.449193 -> 1.449
0.449193 -> 0.4492
0.000449193 -> 0.0004492
I resolved it with this method, there may be a smoother solution.
public static float LimitDigits(float val,int digits) {
if (val == 0) return 0;
return (float)((val<1.0)?Math.Round(val,digits+(int)Math.Floor(-(Math.Log10(val)))):Math.Round(val, Math.Max(0,digits-(int)Math.Floor((Math.Log10(val)+1)))));
}

is this normal in .NET 6 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I feel it's a bug, is it?
decimal s = 30 / 9;
double w = 20 / 9;
Console.WriteLine(s);
Console.WriteLine(w);
and the result is
Is this normal, yes. Is this a bug, no.
You're doing integer math on 30 / 9 which is 3 and then assigning to a decimal which then does the conversion. Same with the second line.
Try this instead:
decimal s = 30m / 9m;
double w = 20.0 / 9.0;
Console.WriteLine(s);
Console.WriteLine(w);
That gives:
3.3333333333333333333333333333
2.2222222222222223
You need to declare your constants in your expression with the correct type. Suffix an m for decimal. And either provide a decimal place or suffix with a d for double.

floats being saved as 0 after calculation [duplicate]

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

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.

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