ganapathy secrets
ganapathy secrets

Reputation: 73

How to calculate percentage in c#

I am trying to calculate percentage of the given long value. The following code is returning zero . How to calculate percentage in c#

long value = (fsize / tsize) * (long)100;

Here fsize and tsize is some Long values.

Upvotes: 4

Views: 27409

Answers (3)

Harry007
Harry007

Reputation: 59

best way to do this

int percentage = (int)Math.Round(((double)current / (double)total) * 100);

Upvotes: 2

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Try this

var value = ((double)fsize / tsize) * 100;
var percentage = Convert.ToInt32(Math.Round(value, 0));

Upvotes: 7

radu florescu
radu florescu

Reputation: 4353

You could try something like this:

double value = ((double)fsize / (double)tsize) * 100.0;

if fsize is int and tsize is int then the division is also an int and 0 is the correct answer. You need to convert it to double to get the correct value back.

Upvotes: 3

Related Questions