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…

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

7 thoughts on “how to show numbers in 0, 1, 2, 3, 4 decimal places in a GridView?”

  1. I want to show four decimal places without rounding off the Amount
    If Amount = 123.321489 after formatting it should be [123.3214] rather than 123.3215.

    Thanks in Advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s