从Excel单元格读取十进制值(在C#中) [英] Reading decimal value from an Excel cell (in C#)
问题描述
从包含小数的Excel单元格中读取值时,我遇到以下问题:如果我在Excel的单元格中输入9.95,则C#中的CellValue.InnerText
返回"9.9499999999999993"
When reading values from Excel cells that contain decimals, I am running into the following issue: If I enter 9.95 in the cell in Excel, CellValue.InnerText
in C# returns "9.9499999999999993"
在知道试图获取这些值的代码不知道是十进制还是数字的情况下,如何获得输入的实际值,即"9.95".
How can I get the actual value that was entered, meaning "9.95", knowing that the code that's trying to get these values does not know ahead of time that it's a decimal or indeed a number.
推荐答案
您无法检索为数字条目输入的实际值.我不认为它存储在任何地方.您看到的值就是存储的值;您还可以检索显示的值(在VBA中将是该单元格的.Text属性),但我认为您无法获得所输入的值.
You cannot retrieve the actual value entered for a numeric entry. I don't believe that is stored anyplace. The value you see is the value that is stored; you can also retrieve the value displayed (in VBA it would be the .Text propery of the cell), but I don't think you can get to the value entered.
问题与以二进制表示某些十进制数字的固有固有可能性有关.
The issue has to do with the inherent impossibility of representing certain decimal numbers in binary.
另请参见对Excel的评论XML格式
这篇关于从Excel单元格读取十进制值(在C#中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!