1

I am reading in an XML file and reading a specific value of 10534360.9

When I parse the string for a decimal ( decimal.Parse(afNodes2[0][column1].InnerText) ) I get 10534360.9, but when I parse it for a float ( float.Parse(afNodes2[0][column1].InnerText) ) I get 1.053436E+07.

Can someone explain why?

4 Answers 4

3

You are seeing the value represented in "E" or "exponential" notation.

1.053436E+07 is equivalent to 1.053436 x 10^7, or 10,534,360, which is the most precise way .NET can store 10,534,360.9 in the System.Single (float) data type (32 bits, 7 digits of precision).

You're seeing it represented that way because it is the default format produced by the Single.ToString() method, which the debugger uses to display values on the watch screens, console, etc.

EDIT

It probably goes without saying, but if you want to retain the precision of the original value in the XML, you could choose a data type that can retain more information about your numbers:

Sign up to request clarification or add additional context in comments.

Comments

1

1.053436E+07 == 10534360.9

Those numbers are the same, just displayed differently.

Comments

1

Because float has a precision of 7 digits. Decimal has a precision of 28 digits.

Comments

0

When viewing the data in a debugger, or displaying it via .ToString, by default it might be formatted using scientific notation:

Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

To format it as exact output, use the R (round trip) format string:

myFloat.ToString("R");

http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.