String format in C #

I have a value from 1 to 10000000. After a value of 10000, I need to show the values ​​as 1E6,1E7,1E8, .... How to set this in string.Format?

Thanks everyone for the answer. Now I can display 1E5,1E6,1E7, .... using the format "0.E0", but I do not want to set the "E" from 1 to 10000.
How to do it?

+6
string format
source share
5 answers

You can use the exponent notation , but I think that it will work for all numbers, and not only for those that are more than 10000. You may need to have a condition to handle this case.

+2
source share

Something like this should do the trick:

void Main() { Console.WriteLine(NumberToString(9999)); Console.WriteLine(NumberToString(10000)); Console.WriteLine(NumberToString(99990)); Console.WriteLine(NumberToString(100000)); Console.WriteLine(NumberToString(10000000)); } // Define other methods and classes here static string NumberToString(int n) { return (n > 10000) ? n.ToString("E") : n.ToString(); } 

=>

 9999 10000 9.999000E+004 1.000000E+005 1.000000E+007 

nb: choose the best name for the function.

+2
source share

Scientific notation? Here are some of them to help: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SpecifierExponent

0
source share

I suggest referring to the value as a float. This way you can use "NumberStyles.AllowExponent" and this will provide exactly what you are looking for.

  string i = "100000000000"; float g = float.Parse(i,System.Globalization.NumberStyles.AllowExponent); Console.WriteLine(g.ToString()); 
0
source share
  String.Format ("10 ^ 8 = {0: e}", 100000000 "); // The" e "will appear lowercase
 String.Format ("10 ^ 8 = {0: E}", 100000000 "); // The" E "will appear in uppercase

If you want it to be prettier, try the following:

  Console.WriteLine ("10 ^ 8 =" + 100000000.ToString ("0. ## E0"));
0
source share

All Articles