In Visual studio 2010, c# .net while doing programming i found that the expressions
var prs1 = (e.X * 100) / panel4.Width;
var prs2 = prs1;
var ans = (prs2 * panel2.Width) / 100;
pb2.Location = new Point(ans, 30);
and another code:
var prs1 = (e.X / panel4.Width) * 100;
var prs2 = prs1;
var ans = (prs2 /100) * panel2.Width;
pb2.Location = new Point(ans, 30);
//this code giving incorrect response
please tell as mathematically both code are equal than why compiler is not giving me same output?
e.X looks like a X coordinate of an event handler. This is an integer value. The same for the .Width property.
Dividing value first may result in loss of precision, especially if the integer division results in 0. Multiplying by 100 afterwards still results in 0.
If you multiply by 100 first, it is more likely that the division is different to 0.
The problem here is rounding.
First part (e.X / panel4.width) of your second code very likely gives 0 as result:
lets say:
e.X = 40 and panel4.width = 250
then you get :
pr1 = (e.X / panel4.width); // 0.16 then it is rounding to 0
pr1 = pr1 * 100 // result is 0
You need to convert your values to double to get result of double then you can convert to "int" yourself later.
pr1 = Convert.ToDouble(e.X) / Convert.ToDouble(panel1.Width) * 100; // result is 16.0
Your problem is not relate to airthmetic expression. Its just because of var keyword.var is used like as generic types.if you initialize val like as int then you can not insert any string value in that or may be something else.
you got var ans like as double or int and you pass this to [pb2.Location = new Point(ans, 30);] which is wrong.because point relate to another type.
Related
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;
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;
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;
I'm counting the results of my database.
If it's lower then 50, i want to divide them by 2.
Example:
if(CountResults < 50)
{
//CountResults = 39
int divided = CountResults / 2; //Results in 19
}
What i want:
if(CountResults < 50)
{
//CountResults = 39
int divided = CountResults / 2; //Results in 19,5
Math.Round(divided, 0);
}
I want to be able to round it upwards and down.
So i get the result 19.5 twice. Once i want it to be 19, and once to be 20..
How do i achieve this?
It's not clear how you are going to use your code twice, but if you want to divide integer into two integer parts just subtract first result from totals:
if(CountResults < 50)
{
//CountResults = 39
int divided1 = CountResults / 2; // 19
int divided2 = CountResults - divided1; // 20
}
First result will use integer division and it will give you result rounded towards zero (19 in your case). Further reading: C# Specification 7.7.2 Division Operator
Second result will give you rest which will be either equal to first result (if there was no rounding), or it will be equal to division rounded from zero (20 in your case).
The rounding part can be accomplished using these 2 nice methods:
Math.Floor brings it to the floor
Math.Celing lifts it to the celing ;)
The calculation part is a little more tricky. This statement:
int divided = CountResults / 2; //Results in 19,5
cannot really be true, or let's say it does not matter what is behind the comma because when it is assigned to the variable int devided it will loose this information and no rounding is anymore required.
When you want a result of type double (meaning e.g. 19,5 ) and you want to round that result, you need at least one of the parameters of the calculation to be of type double double!
Example
double var1 = 39;
int res_low = (int)Math.Floor(var1 / 2);
int res_high = (int)Math.Ceiling(var1 / 2);
note that writing 2 is implicitly seen by the compiler as int and writing 2.0 is implicitly seen as double. So this would yield the same result:
int var2 = 39;
int res_low2 = (int)Math.Floor(var2 / 2.0);
int res_high2 = (int)Math.Ceiling(var2 / 2.0);
I have two values one with a decimal value
and another value with a value which will calculate the percentage of that decimal value
for example:
60 % of 10 = 6
decimal value1 = 10;
decimal percentage = 60;
textbox1.text = ("mathsum here").toString();
How would you calculate this value using the decimal value and value containing the percentage value?
number * percentage / 100
so
10 * 60 / 100 = 6
Maybe it will help you to think of it in this way.
6
-- = .6 (or equivalent to your 60%)
10
In your example you'd like to know how to calculate the numerator (the 6) so assign a variable to it. Let's use X.
X
-- = .6
10
.. and solve for X by multiplying both sides by 10 (in your case).
X * 10 = .6 * 10
------
10
X = .6 * 10
From this I hope you can see that you can take your percentage value and multiply it by your 'decimal' value.
Note that in order to get the .6 you will need to convert your percentage (60) by dividing it by 100.
So our final formula is:
60
--- * 10
100
or using your variables:
percentage
---------- * value1
100
I hope I've added to your understanding even if my formula is similar to the previous answers. I wanted to make sure you understood how the formula was derived.
Good luck!
var result = (percentage/100) * value1;
textbox1.Text = result.ToString();
You mean like this?
textbox1.text = (value1 * percentage/100).ToString();
By the way, toString is written ToString in C# with a capital T.
var answer = value1 * (percentage/100);
Wouldn't this just be
percentage/100m*value
?
To get the percentage amount
decimal Value = 1200;
int percentage = 20; //20%
var result=(percentage/100)*(Value);
I would separate the concerns:
Calculate a portion of your original decimal:
decimal result = (value * percentage) / 100.0;
Provide an appropriate formatter to output the result as a percentage:
text = result.ToString("0.0%");
http://www.dotnetperls.com/percentage
You need to divide by 100.
60% = 60/100.
from question it self answer is clear
60% means 60/100 then calculate it with the value
60 / 100 * 10 = 6 use the logic for variables
textbox1.Text = ((percentage /100) * value).ToString();
or
textbox1.Text = ((percentage * .01 ) * value).ToString();