+ 1

Percentage in C#

How to assign (declare) percentage in C#? And how to use it??

4th May 2020, 6:52 PM
Rifky Niyas
Rifky Niyas - avatar
4 Answers
+ 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 + " %");
4th May 2020, 7:00 PM
A͢J
A͢J - avatar
+ 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);
4th May 2020, 7:06 PM
Ipang
+ 1
I'm not getting what you mean by declaring percentage, could you elaborate further on your thoughts please?
4th May 2020, 6:54 PM
Ipang
+ 1
number * percentage / 100 so 10 * 60 / 100 = 6
5th May 2020, 6:36 AM
SITHU Nyein
SITHU Nyein - avatar