I am copying a value 8.03 from excel sheet and reading that value in C# from the clipboard using the code below. The value that is coming out is 8.0299999999999994 and not 8.03. When I look at the xml as in below the value is 8.0299999999999994. The sheet1.xml which is in the zip file of xlsx also has the value 8.02999999999999994. But when I copy the value to notepad the value gets copied at 8.03. What can I do to copy the value as 8.03 like in notepad?
DataObject retrievedData = Clipboard.GetDataObject() as DataObject;
if (retrievedData.GetDataPresent("XML Spreadsheet"))
{
var sourceDataReader = new XmlTextReader(retrievedData.GetData("XML Spreadsheet") as System.IO.Stream);
var doc = XDocument.Load(reader);
var excelWorksheeet = doc.Element("Worksheet");
XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
XName worksheetName = ns + "Worksheet";
var worksheetElement = doc.Root.Element(worksheetName);
}
The worksheetElement has the value:
<Worksheet ss:Name="Sheet1" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" ss:DefaultRowHeight="15">
<Row>
<Cell>
<Data ss:Type="Number">8.0299999999999994</Data>
</Cell>
</Row>
</Table>
</Worksheet>
The clipboard has support for several formats. Obviously the
XML Spreadsheetformat does a different rounding of the underlying float value than excel does. If you want to get the data exactly as notepad does, try to get thetextrepresentation instead.Of course, copying as
textwill not get all the detailed information thatXML Spreadsheetgives you, but it all depends on what you want to do with the data.An alternative is to get both representations. Get the
XML Spreadsheetformat to get the detailed data and use thetextformat to get a display string.