Exact textual representation of an IEEE “double”

后端 未结 4 1275
星月不相逢
星月不相逢 2021-01-12 14:35

I need to represent an IEEE 754-1985 double (64-bit) floating point number in a human-readable textual form, with the condition that the textual form can be parsed back into

4条回答
  •  广开言路
    2021-01-12 14:50

    The .NET framework has a round-trip format for this:

    string formatted = myDouble.ToString("r");
    

    From the documentation:

    The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.

    This method could of course be recreated in most any language.

提交回复
热议问题