简体   繁体   English

在C#中除以2个long时获得0值

[英]Getting a value of 0 when dividing 2 longs in c#

I am trying to divide the values of DriveInfo.AvailableFreeSpace & DriveInfo.TotalSize to try and get a percentage of it to use in a progress bar. 我正在尝试将DriveInfo.AvailableFreeSpace和DriveInfo.TotalSize的值相除,以尝试获取进度条中要使用的百分比。

I need the end value to be an int as progressbar.value requires an int and the above methods return a long. 我需要最终值是一个整数,因为progressbar.value需要一个整数,并且上述方法返回long。

I have this as the values: 我将其作为值:

164660715520---AvailableFreeSpace 164660715520 ---可用的自由空间

256058060800---TotalSize 256058060800 ---总大小

When I try and set the progressbar.value using this method I also get an error: 当我尝试使用此方法设置progressbar.value时,也会出现错误:

progressBar1.Value=(int)(dInfo.AvailableFreeSpace/dInfo.TotalSize)*100; progressBar1.Value =(int)(dInfo.AvailableFreeSpace / dInfo.TotalSize)* 100;

When I use this code to try and get a value it just returns a 0. 当我使用此代码尝试获取值时,它只会返回0。

label10.Text =  (((int)dInfo.AvailableFreeSpace/dInfo.TotalSize)*100).ToString();

I've even tried this and it doesn't work: 我什至尝试过,但不起作用:

label10.Text = ((dInfo.AvailableFreeSpace/dInfo.TotalSize)*100).ToString();

I know I still have to do some formatting to get it to look good but whenever I run the code it just returns a 0. 我知道我仍然需要进行一些格式化才能使其看起来不错,但是每当我运行代码时,它只会返回0。

Could it have something to do with the conversion from long to int? 它与long到int的转换有关吗?

Integer division returns only the integer part. 整数除法仅返回整数部分。 So 3/5 will be 0. You'll need to treat at least one of the factors as a floating point number. 因此3/5将为0。您需要将至少一个因子视为浮点数。 Try this: 尝试这个:

label10.Text =  (((int)((double)dInfo.AvailableFreeSpace/dInfo.TotalSize)*100)).ToString();

The first way with casting dInfo.AvailableFreeSpace to int right away is not going to work, because the number 164660715520 that you are trying to cast does not fit in 32 bits. 立即将dInfo.AvailableFreeSpaceint的第一种方法将不起作用,因为您尝试转换的数字164660715520不适合32位。 You need to stay in 64 bits as long as you can. 您需要保持尽可能长的64位。

The trick to avoiding floating-point arithmetics is to multiply by 100 first, and then do the division. 避免浮点运算的技巧是先乘以100 ,然后进行除法。

As long as the number times 100 is within the allowed size (and in your case, it is) the result has a good precision: 只要数字乘以100都在允许的大小范围内(对于您而言,是这样),结果就具有良好的精度:

label10.Text = ((100*dInfo.AvailableFreeSpace/dInfo.TotalSize)).ToString();

Demo on ideone (prints 64). 关于ideone的演示(打印64)。

long total = 256058060800L;
long avail = 164660715520L;
var pct = (100*avail/total).ToString();
Console.WriteLine("{0}", pct); // 64

The value is rounding down to 0, as long types cannot have decimal places, only whole integers. 该值四舍五入为0,因为long类型不能有小数位,只能是整数。

Cast as a decimal type before multiplying by 100 在乘以100前转换为decimal类型

For Example (In my case where I am calculating progress percentage for ProgressBar): 例如(在我要为ProgressBar计算进度百分比的情况下):

double someVariable = (double)longVariable1 / (double)longVariable2; double someVariable =(double)longVariable1 /(double)longVariable2;

Code is given below: 代码如下:

public partial class Form1 : Form
{
   public Form1()
    {
         web.ProgressChanged += new WebBrowserProgressChangedEventHandler(web_ProgressChanged);
    }

private void web_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {

        <strong>double t = (100 * (double)e.CurrentProgress / (double)e.MaximumProgress);</strong>
        if (t < 0)
            return;
        int c = (int)t;
        progressBar.Value = c;

        if (c == 100)
        {
            label1.Text = "Done";                
        }
    }
}

make your dInfo.AvailableFreeSpace and dInfo.TotalSize decimal or double type to avoid casts Or 使您的dInfo.AvailableFreeSpace和dInfo.TotalSize为小数或双精度类型,以避免强制转换或

use label10.Text = (((double)dInfo.AvailableFreeSpace/dInfo.TotalSize)*100).ToString(); 使用label10.Text = (((double)dInfo.AvailableFreeSpace/dInfo.TotalSize)*100).ToString();

I might be a bit late but I had this issue when trying to compare timespan's. 我可能有点晚了,但是在尝试比较时间跨度时遇到了这个问题。 Eventually I cast each timepsan to a string and manually computed the ratio 最终,我将每个timepsan转换为字符串并手动计算比例

    private static double Improvement(string efficientTime, string inefficientTime)
    {
        var magnitude = Math.Pow(10, (inefficientTime.Length - efficientTime.Length));
        var time2 = (double) Convert.ToInt32(efficientTime.Substring(0, 5));
        var time1 = (double) Convert.ToInt32(inefficientTime.Substring(0, 5));
        var sigRatio = (time2 / time1);

        var sigPercent = sigRatio * magnitude;

        var improvement = sigPercent;
        return improvement;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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