Question

I have a webpage that gets the data from the db and than displays it online but for some reason if the value is 0.4 it's changing it to 0.400000005960464 and if it's 859.8 it changes it to 859.799987792969. I debugged it and saw that it's reading values correctly from the db so that's not the issue but it's the .aspx page that is messing up the values. This is what I have for that page, it's DataField Component 1 and 2 that are getting changed.

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" 
AutoEventWireup="true" CodeFile="TestData.aspx.cs" 
Inherits="Inventory_TestData" Title="Module Test Data" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" 
Runat="Server">
<ul>
    <li>Item ID:
        <asp:TextBox ID="txtItem" runat="server" AutoPostBack="True" OnTextChanged="txtItem_TextChanged"></asp:TextBox></li></ul>
<li style="text-align: left"><span style="color: #009999">Test Data:</span><asp:ObjectDataSource
    ID="dsrcGetTestData" runat="server" SelectMethod="getTestData" TypeName="TestDataReader" DeleteMethod="deleteData">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="0" Name="nItemID" QueryStringField="Item" Type="Int32" />
    </SelectParameters>
    <DeleteParameters>
        <asp:Parameter DefaultValue="" Name="TestID" Type="Int32" />
        <asp:Parameter DefaultValue="0" Name="TestType" Type="Object" />
    </DeleteParameters>
</asp:ObjectDataSource>
    <asp:GridView ID="gvTestItem" runat="server" AutoGenerateColumns="False" BackColor="White"
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
        DataSourceID="dsrcGetTestData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
        <FooterStyle BackColor="#CCCC99" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />

             <asp:BoundField DataField="Component1" HeaderText="Component1" SortExpression="Component1"   />    
             <asp:BoundField DataField="Component2" HeaderText="Component2" SortExpression="Component2"/>    

        </Columns>
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"  />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White"  />
    </asp:GridView>

 </asp:Content>
Was it helpful?

Solution

You can try adding format string to your columns. For example following

<asp:BoundField DataFormatString="{0:F3}" DataField="Component1" HeaderText="Component1" SortExpression="Component1" />    
<asp:BoundField DataFormatString="{0:F3}" DataField="Component2" HeaderText="Component2" SortExpression="Component2"/>

will limit number to 3 decimal points.

OTHER TIPS

Your issue is probably due floats being stored as an approximation of the number. To get around this, you can use a fixed-precision decimal type, or cast floating-point numbers to a fixed-precision decimal type before you work with them.

Floating-point numbers generally do not have an exact binary representation.... At different times, different numbers of binary digits may be used to represent the number. When a floating point number is converted from one representation to another representation, the least significant digits of that number may vary slightly. Conversion typically occurs when the number is cast from one type to another type... When formatted as a string, the number may not show the expected value.

To minimize these effects, you should use the closest match between numeric types that is available to you. For example, if you are working with SQL Server, the exact numeric value may change if you convert a Transact-SQL value of real type to a value of float type. In the .NET Framework, converting a Single to a Double may also produce unexpected results. In both of these cases, a good strategy is to make all the values in the application use the same numeric type.

as per Floating-Point Numbers in the MSDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top