Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the Standard Numeric Format for percentages include a space?

Tags:

People also ask

What is standard numeric format?

Numeric format specifier (N) The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd. ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol.

What is the ToString format code for percentage?

The percent ("P") format specifier is used to multiply a number by 100. It converts the number into a string representing a % (percentage). In the same way, using (“P1”), would only include a single vale after decimal-point.

What is ToString N in C#?

ToString(String) Returns a string representation of the value of this Guid instance, according to the provided format specifier. ToString() Returns a string representation of the value of this instance in registry format.


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.