views:

364

answers:

2

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 http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx]">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 http://msdn.microsoft.com/en-us/library/0c899ak8.aspx]">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.

+1  A: 

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 %

Aleris
+2  A: 

To change the default PercentPositivePattern value, you must create a new CultureInfo object and use it instead of the default, which is probably "en-us". (Alternately, you can specify "en-za" as the culture in your Web.config, but I don't recommend that for obvious reasons.)

There's a great post on the ASP Forums explaining how to do all of this. See the code written by the third poster (m0brien). I copied his exact code into my master page's Page_Load method, so that I don't have to think about it anymore.

Of course, you could always just do String.Format().Replace(" ", String.Empty), but you'd have to do that throughout your site. Plus, honestly, where's the fun in that when you could roll up your sleeves and play with your globalization values???

David