Hello,
I'm trying to convert a string that is returned in bytes to gigabytes or megabytes.
Ex. 2145374208 bytes to 2gb
Any help is appreciated!
Hello,
I'm trying to convert a string that is returned in bytes to gigabytes or megabytes.
Ex. 2145374208 bytes to 2gb
Any help is appreciated!
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dan BibaudPosted Aug 24, 2007, 9:11 AM
AlanPosted Aug 23, 2007, 4:33 PM
This should do it. Note that I've set the accuracy to one decimal place:
using System;
class Program
{
static void Main()
{
string bytesString = "2145374208";
Console.WriteLine("{0} bytes",bytesString);
ulong bytes = UInt64.Parse(bytesString);
string mb = ConvertToMegabytes(bytes);
string gb = ConvertToGigabytes(bytes);
Console.WriteLine("equals {0}", mb);
Console.WriteLine("equals {0}", gb);
Console.ReadLine();
}
static string ConvertToMegabytes(ulong bytes)
{
return ((decimal)bytes/1024M/1024M).ToString("F1") +"MB";
}
static string ConvertToGigabytes(ulong bytes)
{
return ((decimal)bytes/1024M/1024M/1024M).ToString("F1") +"GB";
}
}