how can I remove zeros from exponent notation

喜欢而已 提交于 2019-12-05 00:57:40

Assuming you need to always show 4 digits after decimal point, try

"0.0000E+0"

so it will show

(0.0000123456).ToString("0.0000E+0"); //1.2345E-5 
(0.0000120000).ToString("0.#E+0");    //1.2000E-5

if you don't need to show 4 digits after decimal points use

"0.#E+0"

so it will show

(0.0000123456).ToString("0.#E+0"); //1.2E-5
(0.0000120000).ToString("0.#E+0"); //1.2E-5

Quoting MSDN:

The case of the format specifier indicates whether to prefix the exponent with an "E" or an "e". The exponent always consists of a plus or minus sign and a minimum of three digits. The exponent is padded with zeros to meet this minimum, if required.

This is with the standard number specifier.

However, with the custom number format, you can set the number of 0's:

987654 ("#0.0e0") -> 98.8e4

For your case, it's

(0.0000123456).ToString("#0.0E0"); //12.3E-6

Edit after BobSort comment

If you need scientific notation, you can specify that you need only one digit before decimal with the following:

(0.0000123456).ToString("0.00#E0"); //1.23E-5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!