Lovedeep Singh
Lovedeep Singh

Reputation: 23

bound field round off the decimal values

I have a bound filed in gridveiw and I need only two letter after decimal, but third letter is round off in second

Value: 2.777

Output: 2.78

Desired output: 2.77

I am using code below

<asp:BoundField datafield="SCPA" 
                headertext="SCPA" 
                HeaderStyle-HorizontalAlign="Center" 
                DataFormatString="{0:N2}">
</asp:BoundField>

Upvotes: 1

Views: 331

Answers (1)

Gauravsa
Gauravsa

Reputation: 6528

Need to create a function in codebehind:

protected object TruncateNumber(object num)
{
     double dnum = Double.Parse(num.ToString());
     dnum = ( (double) ( (int) (dnum * 100.0) ) ) / 100.0 ;
     return dnum;
}

On your aspx side:

<asp:GridView ID="GridView1" runat="server">
        <Columns>
             <asp:TemplateField>
                <ItemTemplate>
                        <%#TruncateNumber(Eval("Number")) %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

Upvotes: 1

Related Questions