Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I remove zeros from exponent notation

I'm using exponential formatting to format a decimal number in C#. For example if the number is

0.0001234567

Formatting with

(0.0000123456).ToString("E4");

Shows

1.2345E-004

How can I remove leading zero from exponent so it read as below?

1.2345E-4
like image 711
AaA Avatar asked Jan 09 '12 06:01

AaA


1 Answers

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
like image 174
CharlesB Avatar answered Oct 10 '22 05:10

CharlesB