代码之家  ›  专栏  ›  技术社区  ›  MatthewD

单元格中的两个十进制浮点变为多个小数

  •  2
  • MatthewD  · 技术社区  · 9 年前

    我在NameValueCollection中有一个值,它是.22 fValue .

    当写入Excel时,调试中的值为fValue=0.22。

    InvoiceWs.Cells["I" + iRow.ToString()].Style.Numberformat.Format = "$#,##0.00";
    fValue = float.Parse(nvcUnitPrice["LG3000"].ToString());
    InvoiceWs.Cells["I" + iRow.ToString()].Value = fValue;
    

    当我打开Excel文件时,我看到该值为0.219999998807907,我无法理解发生了什么。有什么想法吗?

    enter image description here

    2 回复  |  直到 9 年前
        1
  •  3
  •   vbouk    9 年前

    既然你在处理价格,你应该使用十进制类型。来自msdn https://msdn.microsoft.com/en-us/library/364x0z75.aspx .很高兴帮助你

        2
  •  1
  •   John Sensebe    9 年前

    浮点数只有一定的精度。可以表示的最接近0.22的数字是您在表格中看到的数字。最好的办法是使用不同的类型,例如 decimal 对于 fValue 不会受到这种弹劾。

    编辑:如果输入 0.22 here ,你可以明白我的意思。