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.
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.
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.
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:
The Space ProblemCalculated as above based on the phased minimum Company contribution rate of 2 %
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.
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.
I am going to end up using a Custom Numeric Format String, E.g.
myDecimal.ToString("0%")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With