Search code examples
.netformattingfloating-pointtostringnumber-formatting

How to format a double with as many as significant digits as necessary?


I want to format a double value with as many digits as necessary to stay the same value. I do not want to use exponential notation, because the receiver of the string needs floating point notation.

Examples:

12.0 -> "12"
12.20300 -> "12.203"

EDIT: The Round-trip specifier "R" seems to do what I need. At least with the number I tested with, it worked as expected.


Solution

  • The [Round-trip specifier "R"][1] seems to do what I need. At least with the number I tested with, it worked as expected.