Representing Numbers in Exponential form

前端 未结 2 450
北海茫月
北海茫月 2021-01-02 23:23

I have a requirement to convert the following types of decimal numbers to exponential format

Number 0.00001formatted as 0.01E-04

相关标签:
2条回答
  • 2021-01-02 23:27

    I think you want something like:

    string text = dValue.ToString("0.###E+00");
    

    (Change the number of # characters to change the number of decimal digits before the E.)

    You can do this with a compound format specifier as well by calling string.Format, but personally I'd use a simple one unless you need to put other text round it anyway, in which case you'd use use something like:

    string text = string.Format("Before {0:0.0###E+00} After", dValue);
    
    0 讨论(0)
  • 2021-01-02 23:35

    You'll need to use a custom format string to specify how to format your decimal:

    string.Format("{0:0.##E+00}", dValue);
    

    You can find more about custom numeric format strings on the MSDN here. There's a specific section on Exponent formats.

    0 讨论(0)
提交回复
热议问题