我對數學有些生疏,所以我希望有人能幫助我。使用下面的代碼我想要做到以下幾點:根據安裝的內存量,我想顯示可用內存的百分比,而不是剩下多少兆字節。如何四捨五入數字並以百分比顯示?
private void timer1_Tick(object sender, EventArgs e)
{
string memory;
int mem;
memory = GetTotalMemoryInBytes().ToString();
mem = Convert.ToInt32(memory);
mem = mem/1048576;
progressBar2.Maximum = mem;
progressBar2.Value = mem - (int)(performanceCounter2.NextValue());
label2.Text = "Available Memory: " + (int)(performanceCounter2.NextValue()) + "Mb";
}
//using Microsoft visual dll reference in c#
static ulong GetTotalMemoryInBytes()
{
return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
}
任何特定的原因你獲取總內存,將其轉換爲字符串,然後將字符串轉換回數字? –
嗨馬克...我是C#的新手,因此我的技能不好:) –