Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# aspx页面在线显示时更改浮点值的问题_C#_Asp.net_Floating Point Conversion - Fatal编程技术网

C# aspx页面在线显示时更改浮点值的问题

C# aspx页面在线显示时更改浮点值的问题,c#,asp.net,floating-point-conversion,C#,Asp.net,Floating Point Conversion,我有一个从数据库获取数据并在线显示的网页,但由于某种原因,如果值为0.4,它将更改为0.400000005960464,如果值为859.8,它将更改为859.799987792969。我调试了它,发现它正确地从数据库中读取了值,所以这不是问题所在,而是.aspx页面弄乱了值。这就是我在那个页面上看到的,数据域组件1和2正在被更改 <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="tru

我有一个从数据库获取数据并在线显示的网页,但由于某种原因,如果值为
0.4
,它将更改为
0.400000005960464
,如果值为
859.8
,它将更改为
859.799987792969
。我调试了它,发现它正确地从数据库中读取了值,所以这不是问题所在,而是
.aspx
页面弄乱了值。这就是我在那个页面上看到的,数据域组件1和2正在被更改

<%@ 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>

  • 项目ID:
测试数据:
您的问题可能是由于浮点数被存储为数字的近似值。为了解决这个问题,您可以使用固定精度十进制类型,或者在使用浮点数之前将其转换为固定精度十进制类型

浮点数通常没有精确的二进制 代表。。。。在不同的时间,可能需要不同数量的二进制数字 用于表示数字。当浮点数为 从一个表示转换为另一个表示,最小 该数字的有效数字可能略有不同。转化 通常在将数字从一种类型转换为另一种类型时发生 键入。。。当格式化为字符串时, 该数字可能不会显示预期值

为了最小化这些影响,您应该使用最接近的匹配 您可以使用的数字类型。例如,如果你是 使用SQL Server时,如果 将实数类型的Transact-SQL值转换为浮点类型的值。在里面 在.NET Framework中,将单个文件转换为双文件也可能会产生 意外的结果。在这两种情况下,一个好的策略是 应用程序中的所有值都使用相同的数字类型

根据MSDN中的。

您可以尝试添加到列中。例如:

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


将数字限制为3个小数点。

这很有意义。TY.将值存储为的数据库是什么数据类型?aspx页面上使用的是什么数据类型?MikeSmithDev的回答可能指出了问题所在,但根据数据类型转换的方式,解决方案只能使用几样东西。@akousmata数据库将值存储为实值,并且在从数据库读取数据时将其转换为浮点值。