【发布时间】:2011-01-11 09:25:42
【问题描述】:
我正在尝试使用下面的代码将字节转换为 KB/MB/GB,但是,我似乎无法让它工作。配额的值为60000000000。
public static double BytesToKilobytes(this Int32 bytes)
{
return bytes / 1000d;
}
public static double BytesToMegabytes(this Int32 bytes)
{
return bytes / 1000d / 1000d;
}
public static double BytesToGigabytes(this Int32 bytes)
{
return bytes / 1000d / 1000d / 1000d;
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XDocument xDocument = XDocument.Parse(e.Result);
listBox1.ItemsSource = from query in xDocument.Descendants("service")
select new Service
{
type = query.Attribute("type").Value,
id = query.Element("id").Value,
plan = query.Element("username").Value,
quota = query.Element("quota").Value.BytesToGigabytes, };
}
以上代码产生的错误是:
"'string' 不包含'BytesToGigabytes' 的定义,并且找不到接受'string' 类型的第一个参数的扩展方法'BytesToGigabytes'(您是否缺少 using 指令或程序集引用?)"
【问题讨论】:
-
可能不是你的问题,但是如果 quota 的值是 60*10^9,它不适合 Int32。您需要在这里使用 Int64(又名 long)。
标签: c# linq itemssource