Format bytes as KB, MB, GB, etc.

Here's a useful little chunk of code to put in your toolbox.  I need to display file sizes to users, and I want to format the displayed file size as you'd see it in Explorer -- 10.1 KB, 23.31 MB, and so on (not 112230103 bytes, for example).  After a quick google search, I turned up a post on that got me really close.  I cleaned up a couple of issues and built a unit test to make sure things were working correctly, and it's ready to go.  Read on to see the working code.

/// Format bytes suitably for display to user (ex: 12.3 KB, 34.41 MB).
/// If you need to handle sizes > Exabyte size, you'll have to modify the
/// class to accept ULong bytes and format accordingly.
/// Based on code as published here:
/// By Bart De Smet --
/// Algorithm fixed to handle KB correctly and modified to use Long
/// instead of int to handle larger file sizes.

public struct FormattedBytes
#region ctor
public FormattedBytes(long size)
_bytes = size;
#region Bytes property
private long _bytes;
/// Get or set bytes.

public long Bytes
get { return _bytes; }
set { _bytes = value; }

public override string ToString()
double s = _bytes;
string[] format = new string[] {"{0} bytes", "{0} KB",
"{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB"};
int i = 0;
while (i < format.Length @@ s >= 1024) {   //replace @@ w/ ampersands
s = (long) (100 * s / 1024) / 100.0;

return string.Format(format[i],s);

And here's the unit test I'm using to test the routine:

/// A test for FormattedBytes ()

public void FormattedBytesTest()
FormattedBytes bs = new FormattedBytes();
Assert.AreEqual("0 bytes", bs.ToString(), "FormattedBytesTest - incorrect value (1).");
bs.Bytes = 10000L;
Assert.AreEqual("9.76 KB", bs.ToString(), "FormattedBytesTest - incorrect value (2).");
Assert.AreEqual("1.17 MB", new FormattedBytes(1230000L).ToString(), "FormattedBytesTest - incorrect value (3).");
Assert.AreEqual("434.87 MB", new FormattedBytes(456000000L).ToString(), "FormattedBytesTest - incorrect value (3).");
Assert.AreEqual("73.48 GB", new FormattedBytes(78900000000L).ToString(), "FormattedBytesTest - incorrect value (4).");
Assert.AreEqual("1.11 TB", new FormattedBytes(1230000000000L).ToString(), "FormattedBytesTest - incorrect value (5).");
Assert.AreEqual("1.09 PB", new FormattedBytes(1230000000000000L).ToString(), "FormattedBytesTest - incorrect value (6).");
Assert.AreEqual("1.06 EB", new FormattedBytes(1230000000000000000L).ToString(), "FormattedBytesTest - incorrect value (7).");

Here’s some more of the value Microsoft sees in Yahoo!

Image via CrunchBase, source unknown

Last time, I hinted that there was more than Google-phobia driving this attempted purchase, and there is.  In addition to vaulting overnight to a relevant search power, there are a few other areas where a combined company would become a real market force.

Start with email.  Even though GMail is growing quickly, Yahoo! still has a ton of email customers.  Add those to Microsoft's Live email, and the combined market share of these two email platforms is on the order of 80%.  That's a lot of captive desktop time where ads could be shown - if Microhoo can maintain that market share.

Then there's portal traffic. Yahoo's home page is a landing spot for a lot of users, and I have a hunch that these are users in the most stable demographics on the web.  All of those eyes are a valuable asset, not just for selling ads, but to serve as a launching pad for new initiatives.  Check out this article on readwriteweb where they talk about the impact of a recently-launched feature called "Buzzing Now".  According to the article,
  • has sent approximately 16 million total referrals to just a subset of the publishers in the beta during the first two weeks via “Buzzing Now” links in the Featured section of the homepage.
  • reached over 1 million uniques in one day for the first time in the company's 12-year history, after linked to one of their highly "buzzed" stories.
  • US Magazine: Referral visits from the Yahoo! homepage accounted for 32% of total visits that day.
Image representing Yahoo! as depicted in Crunc...Go read the article - there are more traffic numbers there.  My point is that when Yahoo! still has the eyeballs and the clout to swing traffic like this, they're a jewel worth getting excited about.
Image representing Microsoft as depicted in Cr...Of course, Microsoft would have their work cut out for them.  The mere fact that despite all this traffic, Yahoo's financials have lagged expectations begs the question of how Microsoft could improve on that performance.  There's also the issue of culture.
Detractors of the Microhoo deal believe that Yahoo's open culture is going to clash mightily with Microsoft's, but I think they're wrong.  I've seen a big change in Microsoft over the last few months.  There's a greater embrace of openness than they've ever shown, and it's taking root and flourishing.  Microsoft is ready for Yahoo's sort of culture, and I really think there are parts of Yahoo! that could stand a little infusion of Ballmer's enthusiasm - maybe even a swift kick.  This could be a pretty cool partnership.
Reblog this post [with Zemanta]

Why you should care if Microsoft buys Yahoo?

Anyone not living under a rock knows that Microsoft is trying to buy Yahoo!, but so what?  What difference could it possibly make to an end-user or a software developer whether Yahoo! remains independent or not?

Clearly, this is a buyout of historic proportions.  The size alone is impressive: at $45B, this is a serious chunk of change and a sizable improvement over the market valuation of Yahoo! prior to Microsoft's offer.  But this buyout isn't notable just for size.  This is a pivotal moment in the growth and maturation of the net: a marquis player is quite possibly going to cease to exist independently, and another is at a "make or break" moment.  As far as brands go, this is an impact on the order of seeing Netscape fade away or AOL get gobbled up by Time Warner. Like AOL, it's quite conceivable that the brand will live on for a long time, but it's clear that it'll never again live with the vigor that it's had in the past.

So just like that, Microsoft is set to gobble up Yahoo!, and the giant from Redmond marches inexorably onward, right?  Not so fast.  Remember how that AOL / Time-Warner deal worked out?  Other industries have shown us the dangers of mega-mergers, as well - Sprint / Nextel has been in the news a lot lately as Sprint slowly continues its death spiral.  Most analyses of Sprint's current woes begin with theNextel takeover and end with gigantic accounting charges to reflect this failure and a mass exodus of (mainly ex-Nextel ) subscribers.  These failures can't be overlooked; Microsoft could catch a world-class case of indigestion if they're not careful.

So why is Microsoft doing this?  Nobody's holding a gun to Ballmer's head, right?  Or are they?  Conventional wisdom holds that Microsoft is making this move strictly out of fear of Google.  Microsoft is trying to buy search traffic (and the associated advertising revenue) because they haven't been able to capture that market share directly.  There's more to it than this, of course, and I'll go into a little more depth in a future post.

In the mean time, keep watching the developments in this buyout, because win or lose, this battle is going to change the face of software.

Reblog this post [with Zemanta]

Greenspan sneezes…

Alan Greenspan's in the news again after he was quoted as saying the current economic meltdown Is "the most wrenching since the end of the second world war."  Now, I know this isn't directly related to software design, but since I've got a degree in Finance, I can't help watching this train wreck, and let's face it - this "situation" has now reached the point where it's impacting the economy as a whole, including software.

Alan Greenspan, KBE, PhD (born March 6, 1926) ...I'll be honest - I don't think it's too beneficial for Greenspan to be skulking around the funeral parlor right now offering commentary on the medical care of the deceased. This man was single-handedly responsible for the most prolonged, sustained, and stable economies in the history of this country, and for that, we're all eternally grateful.  But he's got to realize that he's still incredibly quotable, and the best thing he can do for the economy right now is to just shut up and let the Fed work.  You don't see Bill Gates offering opinions to the Wall Street Jounal about how Ballmer's doing, and I'll bet you won't see Favre giving interviews next year about the play of their new quarterback, and we don't need to hear from Greenspan about how the economy's doing, either.  I know this is the way politics is done - look at the way Bill is campaigning for Hillary.  But Greenspan was always above that, so to see him doing this now is disappointing.

Finance, of course, suffers from its own case of jargon overload, so if you're looking for a really clearly-spoken commentary on our current mess, check out this interview with Princeton economist Paul Krugman.  It's light on financial jargon and deep on content, including a really interesting jab at Greenspan about what he might have done to limit the severity of the subprime mess.


Reblog this post [with Zemanta]

Google Apps trail MS Office, but for how long?

googlemirror This morning, I read a blog post describing how to add page numbers to a Google Docs document.  The crux of the technique was to embed some HTML into your doc such that page numbers are generated when the doc is printed via PDF.  "That's it!" I thought.  That's why Google Apps aren't ready to take down MS Office.

googleprintdialogThen, this afternoon, I just happened (dumb luck, I swear) to use Google Docs to print a draft of an article I'm working on, and I saw this dialog with - you guessed it - an easy way to add page numbers.   And just like that, I saw the problem for Microsoft.  Google, it's true, isn't there yet, but they're closing the gap -- fast.

Microsoft is clearly working hard to neutralize the Google Docs threat with their Office Live Workspace, but I'll bet they're glancing nervously in their rear-view mirrors constantly.

Reblog this post [with Zemanta]