Wednesday, October 08, 2008

string.Format returns the wrong kind of whitespace

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...

2 comments:

Anders said...

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.

Andreas said...

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