-1

I am reading in a value from a PLC via a third party library, however when saved as a double the value appears to be in scientific notation.

The value in the PLC is 1.234 however, when debugging the application, the value being stored in the double is 5.27326315571927E-315

I am displaying this in a label, however I want to display it as 1.234 rather an as scientific notation.

How can I convert this?

Gavin Coates
  • 1,366
  • 1
  • 20
  • 44

1 Answers1

2

As a wild guess, I think you should read 4 bytes(float) from your library not double(8 bytes).

Since 5.27326315571927E-315 is almost zero.

double d = 5.27326315571927E-315;
byte[] b = BitConverter.GetBytes(d);
float f = BitConverter.ToSingle(new byte[] { b[0], b[1], b[2], b[3] }, 0); 

f is 1.2345 now

L.B
  • 114,136
  • 19
  • 178
  • 224
  • Interesting! I changed it to convert the value to a float, and I get the value 1.234 now. The sample code they provided states to convert to a double, and I have done this on a previous project and it worked fine... but not this time! Thanks! – Gavin Coates Apr 08 '12 at 22:34