Working percentage in c# - c#

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();

Related

changing celsius to fahrenheit and vice versa in c# [duplicate]

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;

Lamda expression not working on String.Count() [duplicate]

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;

basic maths calculations in c# [duplicate]

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;

Doing Math Operation using MVC Razor

what i try to do is a simple math operation in my project but something works wrong because i get always 0. So my question is:
how can i do this math operations using mvc razor?
what i try is this:
#{
decimal a = 2 / 4; //--> result 0,5
int b = a * 100 //--> 50
}
the problem is that the first result of the variable a give me 0,5 and i need this value in decimal,
but then i wanted to multiplicate it with 100 that gives me the 50 of datatype int.
hmm, but i canĀ“t figured it out how to do that..
can someone give me a hand with this pls??
You are performing Integer division which gives you Zero here yYou need to convert one of the value into decimal while performing division.
Try This:
decimal a = 2.0M / 4;
int b = Convert.ToInt32(a * 100);
The following works:
decimal a = 2m / 4m; //--> result 0,5
int b = (int)(a * 100m);

Retrieve 40.7288 from 2240.7288 (miss first two values)

I'm converting digital minute values to digital degrees (longitude and latitude), how would I come about retrieving all values apart from the first two? From here I can calculate values, such as 5322.72233N and 00127.5333W, to another format. I've looked at Math.Truncate (Best way to get whole number part of a Decimal number).
Let's calculate latitude 5322.72233
22.72233 / 60
+53
Final result is 53.3787055
Calculating the longitude with value 00127.5333
(Along the lines of... (calculation hasn't been checked thoroughly yet))
27.5333 / 60
+1
x -1
Final result
I'm sure it's simple.
You could just use the % operator (modulus) to remove any digits beyond 100 like this:
var input = 5322.72233m;
var output = input % 100m; // 22.72233
From that point the rest of the math should be pretty easy.
You can try to get the remainder as follows:
double a = 5322.72233;
double b = 100;
double c = a % b;
Next:
c = c / 60 + 53; // 53.3787055

Categories