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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Aug 23, 2007, 12:33 PM EDT
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";
}
}
Dan Bibaud6 reputationPosted Aug 24, 2007, 5:11 AM EDT