+ 1
Percentage in C#
How to assign (declare) percentage in C#? And how to use it??
5 Respostas
+ 2
Rifky Niyas Why you would assign. Just simply print when you are showing output.
int x = 10;
int y = 20;
int z = (x * 100) / (x + y);
Console.Write("z = " + z + " %");
+ 2
Rifky Niyas
One thing to remember, `int` stores whole numbers. If you want to calculate or store percentage then you need a type that supports fraction, and those are `float` or `double`.
Example (paste in main method body):
float half = 0.5f;
double quarter = 0.25;
Console.WriteLine("a half is {0}, a quarter is {1}", half * 100, quarter * 100);
+ 1
I'm not getting what you mean by declaring percentage, could you elaborate further on your thoughts please?
+ 1
number * percentage / 100
so
10 * 60 / 100 = 6