Search code examples
c#string-formattingscientific-notation

Displaying a scientific notation double in a more readable format


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?


Solution

  • 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