ToString argument error

I am trying to format a double into exponential format:

            double pe = 0.01;
            string sss = pe.ToString("0.#E+00");

I get exception “Exception was thrown: System.ArgumentException”

I also get the error if I use

String.Format("{0:0#E+00}", pe)

@Joel_Riley perhaps document what formatting options are supported?

I am having a similar issue. Say I have an enumeration:

enum Color { Red, Green, Blue },

Create an instace of the enum:

private Color color = Color.Green;

Then the 2 lines below through “Exception was thrown: System.ArgumentException”:

string ColorName = color.ToString();
ColorName = Color.red.ToString();

There is known issue with enum and “ToString()”

Unfortunately, TinyCLR can’t support everything you’ll find in desktop .NET C#. We had to make some compromises to fit everything on small embedded systems and still leave room for your application.

There’s always a workaround – what about this:

double pe = 0.01;
double exponent = System.Math.Floor(System.Math.Log10(System.Math.Abs(pe)));
string sss = (pe / System.Math.Pow(10, exponent)).ToString() + "E" + ((exponent < 0.0) ? "" : "+") + ((int)exponent).ToString("D2");

This seems to work, but somebody will probably come up with a solution using half the code. :nerd_face:

3 Likes