The Back Story
I have some decimal values which I am displaying as strings on a web page as part of a larger string. I started off using a Standard Numeric Format String to output this. E.g.
myDecimal.ToString('P0')
The 0 after the P tells it I want no decimal places. This works as far as it goes, in that my ouput ends up looking something like:
Calculated as above based on the phased minimum Company contribution rate of 2 %
The Space Problem
I really want to get rid of that space between the number and the percentage sign as in some cases it ends up splitting across lines. And also, I prefer the % to butt up to the number.
Possible Workarounds
1. html / css solution
I could put a <nobr> tag or a <span style='white-space: nowrap;'> around it. But, that feels awkward, and anyway I prefer the % to butt right up to the number as I think it looks better. It’s how we would write it in reports in this neck of the woods, so it’s how I want it on the web page.
2. Custom format string
I am going to end up using a Custom Numeric Format String, E.g.
myDecimal.ToString('0%')
The Question
Is it more common to display percentages with a space between the number and the percentage sign? This would surprise me, but it could be.
Is there a way to tell the Standard Numeric Format String I don’t want the space?
Is there any disadvantage to using a Custom Numeric Format String over a Standard Numeric Format String?
Ok – I admit it, that was more than one question – Extra Credit if you answer them all.
Looking at http://www.nasdaq.com/ or http://finance.yahoo.com/ it seems that the percentage sign right after the number with no space inbetween is common 🙂
Have a look at http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.percentpositivepattern.aspx
It probably depends on culture, the default (invariant) beeing n %