简体   繁体   中英

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.

Try this

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

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.

最好的办法

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM