# GridView Decimal Format

I have a GridView with a decimal value in it This decimal value can be up to 4 digits to the right of decimal point but how do I get it to display only two digits if the far right 2 are 0's  in other words say I have a decimal that is .21 and one that is .212  how I currently have the Gridview setup it will display the values as .2100 and .2120

This is the formula I used for the data format {0:D}
C#ASP.NET

Last Comment
namansshah
dstanley9

use {0:N4}.   D is for integral types:

http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx
namansshah

http://msconline.maconstate.edu/tutorials/ASPNET2/ASPNET07/aspnet07-01.aspx

Format String Description
{0:Cn} Currency. Displays numeric values in currency format with a leading dollar sign; n indicates the number of decimal places. If n is omitted, the default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of digits desired in the resulting string. If necessary, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates the desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the desired number of decimal places. Commas are inserted between each group of three digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates the desired number of decimal places.
{0} Text. Displays a text string.
Scripter25

Ok Team I have tried it each way you have suggested and ways that I found on the sites that you have sent me. Now I know that one of these ways should have worked but something is going wrong.
It is not changing format at all. No matter what I do even if I try to force feed a % sign into it.
I am not hard coding it into the page but instead using the Fields editor to change the DataFormatString property
Scripter25

The Data Type in the database is  decimal(18, 4)   should I try to go with something else? my overall goal of this project is making a financial calculator for my sister so I figured seeing as how a database does not have a percentage type I could always use the decimal(18, 4) type
Scripter25

I have changed the DB data type to float with same result. also just fyi maybe this has something to do with it. I have the Gridview in an Updatepanel
dstanley9

Can you post the html for your GridView (everything between the <asp:gridview> tags?  Something else may be going on.
Scripter25

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Style="position: static">
<Columns>
SortExpression="INT" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Balance" HeaderText="Balance" SortExpression="Balance" />
<asp:BoundField DataField="Available" HeaderText="Available" SortExpression="Available" />
<asp:BoundField DataField="AmountDue" HeaderText="AmountDue" SortExpression="AmountDue" />
<asp:BoundField DataField="Rate" DataFormatString="{0:P2}" HeaderText="Rate" SortExpression="Rate" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Acount_Num" HeaderText="Acount_Num" SortExpression="Acount_Num" />
<asp:BoundField DataField="DueDate" HeaderText="DueDate" SortExpression="DueDate" />
</Columns>
</asp:GridView>
dstanley9

Are you looking at it in edit mode?  (from MSDN docs at http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx):

By default, the formatting string is applied to the field value only when the data-bound control that contains the BoundField object is in read-only mode. To apply the formatting string to field values while in edit mode, set the ApplyFormatInEditMode property to true.
Scripter25

applyformatineditmode="true"

ok I did that and still same result
Scripter25

<asp:BoundField DataField="Rate" DataFormatString="{0:p}" HtmlEncode="false" HeaderText="Rate" SortExpression="Rate" />

Found the answer HtmlEncode="false"  but it cannot have applyformatineditmode="true" included or else it wont work
namansshah

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY