0

I've created a small program that takes a scientific notation value (i.e. 1x10^10), and displays its full value (I'm practising parameter passing by the way, hence why I created a separate subroutine):

static void Main(string[] args)
    {
        Console.Write("Enter a scientific notation number: ");
        string num = Console.ReadLine();
        Console.Write(ConvertString(num));
        Console.ReadKey();
    }

    static double ConvertString(string num)
    {           
        string[] Array = num.Split('x');
        string[] Array2 = Array[1].Split('^');

        double multiplier = (Convert.ToDouble(Array[0]));
        double value = (Convert.ToDouble(Array2[0]));
        double power = (Convert.ToDouble(Array2[1]));

        double conversion = multiplier * Math.Pow(value, power);
        return conversion;         
    }

It works fine when 'power' is less than 15. However, when it's greater than or equal to 15, the console displays 'E+x' (where 'x' is 'power') instead of the full value. For example:

1x10^5: 100000
1x10^10: 10000000000
1x10^15: 1E+15

This is the same case for negative powers as well. When 'power' is greater than or equal to -5, it displays 'E-x' instead:

1x10^-1: 0.1
1x10^-2: 0.01
1x10^-5: 1E-05

Is there any way to prevent this? Thanks.

ZephyrLM
  • 1
  • 1
  • There is also an answer here: https://stackoverflow.com/questions/14964737/double-tostring-no-scientific-notation – Cosmin Ioniță Feb 28 '18 at 14:51
  • `System.Double` is only accurate to 15 places, try using `System.Decimal` –  Feb 28 '18 at 14:52
  • This worked very well: `Convert.ToDecimal(doubleValue).ToString()`. Thanks for pointing me to it... I wouldn't have written all this if I found that beforehand! :D – ZephyrLM Feb 28 '18 at 15:03
  • @ZephyrLM No problem, be careful because the range is smaller `(-7.9 x 10^28 to 7.9 x 10^28) / (10^0 to 10^28)` –  Feb 28 '18 at 15:06
  • My maximum range would be around 10^23 anyway, so all is well. – ZephyrLM Feb 28 '18 at 15:10

0 Answers0