ILearnable .Net

October 8, 2008

string.Format returns the wrong kind of whitespace

Filed under: Uncategorized — andreakn @ 23:16

ok, this one is weird:

I write a custom util method to format money strings,

it relies on string.Format(“{0:n}”, someValue).

I cover it with unit tests to verify it works, and when I run them

I get this little gem:

It turns out that the whitespace returned from string.Format is character 160, whereas the whitespace you normally get is character 32.

So I change my method to replace all char 160 into char 32, and voila! all the tests pass…

what’s up with that? I didn’t even know there were two chars in .Net that both returned whitespace…



  1. Char 160 is a non-breaking space. This is the one you want to use in this scenario since you don’t want a line break to appear in the middle of your monetary value if it happens to be at the end of a line.

    Comment by Anders — October 9, 2008 @ 00:36 | Reply

  2. still when it breaks my Asserts I still find it a bit sickening.

    Comment by Andreas — October 9, 2008 @ 05:58 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: