如何将 Double 作为字符串输出到 x 个小数位且没有尾随零

How to output a Double as a String to x decimal places with no trailing zeroes

我需要将具有不同小数位 (DP) 的各种数字输出为具有 6 DP 的字符串,除非有尾随零。例如:

0.123456789 = 0.123456

0.1234 = 0.1234

0.0 = 0

我正在尝试使用 "input.ToString("在此处插入数字格式代码")" 但我要么得到正确数量的 DP 但所有尾随零 (0.000000) 要么根本不起作用。有什么建议吗?

这里的理想解决方案是上面建议的使用 .ToString("0.######") 格式化输入:)