Home > ASP .NET > how to show numbers in 0, 1, 2, 3, 4 decimal places in a GridView?

how to show numbers in 0, 1, 2, 3, 4 decimal places in a GridView?

When you have a GridView control and you bind it to a decimal field (for example), you can manipulate the format of the string shown by using the format specifier in the BoundField or in the Eval function in the TemplateField.

Here’s how you do it. Use the f with a number such as f0 for no decimal places, f2 for 2 decimal places and so on so forth.

<asp:BoundField DataField=”prodPrice” HeaderText=”Price” HtmlEncode=”False” DataFormatString=”{0:f0}” />

Or in TemplateField

<asp:TemplateField HeaderText=”Price”>
<ItemTemplate>
<%#Eval(“prodPrice”,”{0:f0}”)%>
</ItemTemplate>
</asp:TemplateField>

You can change the f<number> to any value to get numbers like 2 with f0 and 2.0 with f1 and 2.00 with f2 and so on…

  1. Shrirang
    December 9, 2008 at 10:56 am | #1

    Very Good Answer!!!!

  2. March 11, 2009 at 12:08 pm | #2

    Good tips. I was wondering if I could do it in the way {0:c} works for currency. Now got it. Thanks.

    • Imran Akram
      March 11, 2009 at 12:11 pm | #3

      Its always a pleasure. Thanks for dropping by.

  3. Mohammed
    March 19, 2009 at 9:08 pm | #4

    It helped.. keep it up.

    • Imran Akram
      March 20, 2009 at 5:03 am | #5

      Appreciate the comment. Thanks

  1. No trackbacks yet.