Double to string conversion without scientific notation

前端 未结 17 1513
無奈伤痛
無奈伤痛 2020-11-22 15:33

How to convert a double into a floating-point string representation without scientific notation in the .NET Framework?

\"Small\" samples (effective numbers may be of

17条回答
  •  被撕碎了的回忆
    2020-11-22 16:10

    You could cast the double to decimal and then do ToString().

    (0.000000005).ToString()   // 5E-09
    ((decimal)(0.000000005)).ToString()   // 0,000000005
    

    I haven't done performance testing which is faster, casting from 64-bit double to 128-bit decimal or a format string of over 300 chars. Oh, and there might possibly be overflow errors during conversion, but if your values fit a decimal this should work fine.

    Update: The casting seems to be a lot faster. Using a prepared format string as given in the other answer, formatting a million times takes 2.3 seconds and casting only 0.19 seconds. Repeatable. That's 10x faster. Now it's only about the value range.

提交回复
热议问题