Search code examples
c#genericstype-conversionscientific-notation

C# conversion issue from a generic value to string


I am facing quite an odd issue,....

I have got a code which reads XML and converts each value irrespective of what type it is for example, int, float, double or a String it self to a String value and then stores it into a String variable.

String column = System.Convert.ToString(values.GetValue(rowNum, colNum))

problem I have is, lets say if "values.GetValue(rowNum, colNum)" returns 0.000003825, then when ran, the value that gets converted and stored in "column" is "3.825E-06" which is in scientific notation which I do not really want,

I want "column" to store value 0.000003825 in string format, how do I do that?

thanks


Solution

  • OK, I have fixed this now..

    It works a treat irrespective of what the type of value we pass through obj,

    thanks.

            Object obj = -0.00002357467;
            String value = obj.ToString();
            String type = obj.GetType().ToString();
    
            if (type.Equals("System.Double")&&value.Contains("E-"))
            {
                double doubleValue = (double)obj;
                value = doubleValue.ToString("0.############################"); //thanks @Matthew Watson
            }
    
            Console.WriteLine(value);   //prints -0.00002357467