Craig Gibbons' Lifeblog lifeblog://


ASP.NET and C# – Formatting a proportion as a percentage in a DataGrid using DataFormatString

This really shouldn't be too difficult, but I struggled with it for a while before happening upon the solution while wildly plugging in format strings. All I was trying to do was format a number like 0.5 as 50% in a DataGrid by using the inline DataFormatString={0} syntax. Turns out the solution is quite simple and quite logical as follows: DataFormatString="{0:#0%}". The great thing about this, is that % is a special custom format string character and will automagically multiply your number by 100 to get the percentage value. Of course you can also specify this format string in any ToString() call, e.g. proportion.ToString("#0%")

.NET rocks!

Filed under: Tech Leave a comment
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.