0

I am having to loop throw 00.000000000000001 to 100 however whenever I do this and try and show it as a string it goes to numbers like 2E-15 and stuff, I have a feeling this is just how its represented however how can I get the actual number.

Jogh Noe
  • 21
  • 2

2 Answers2

2

You can use String.Format to determine how your double is displayed:

double d1 = 00.000000000000001;
double d2 = 00.0001;

string text1 = String.Format("{0:000.000000000000000}", d1);
string text2 = String.Format("{0:000.###############}", d1);
string text3 = String.Format("{0:000.000000000000000}", d2);
string text4 = String.Format("{0:000.###############}", d2);
Console.WriteLine(text1+" - "+text2);
Console.WriteLine(text3+" - "+text4);

see MSDN - Custom Numeric Format strings

Johan Donne
  • 3,104
  • 14
  • 21
0

You could just use decimal type instead of double, try this code below:

using System;

class test
{
static void Main()
{
    for(decimal i = 0.000000000000001m; i < 100; i += 0.000000000000001m)
        {
            Console.WriteLine(i);
        }
}


}
fraser jordan
  • 114
  • 1
  • 9
  • `0.000000000000001` is a perfectly valid double - it is `1E-15`. A double can only store up to 16 _significant digits_. `1E-15` only has _one_ significant digit. – D Stanley Aug 21 '15 at 03:49
  • Woops, my bad, will edit my answer so as to not give out mis-information, thanks! :) – fraser jordan Aug 21 '15 at 04:29
  • Actually doubles can be used, but will introduce rounding errors when working wit base 10 numbers. Depending on the applocation, decimal might be a better choice: http://stackoverflow.com/questions/803225/when-should-i-use-double-instead-of-decimal – Johan Donne Aug 21 '15 at 05:46