2

Consider the following code:

var rateUsed = 0.00000001;
var rateUsedConvertToString = Convert.ToString(rateUsed);
var rateUsedToString = rateUsed.ToString();

Instead of my strings being "0.00000001", they are both "1E-08". I don't have control over the initial type of rateUsed, it is a double, and I want the exact string representation of the double.

Why does this happen, and how would I get 0.00000001 as a string, if this started off as a double value?

JMK
  • 27,273
  • 52
  • 163
  • 280
  • Try giving it an explicit type for rateUsed. It may be inferring a different type than you are expecting. – StingyJack Aug 21 '13 at 12:26
  • 3
    Anyone care to explain why the downvoting? – StingyJack Aug 21 '13 at 12:27
  • 2
    This isn't about accuracy; 1E-08 is ***exactly*** 0.00000001; this is simply a formatting question. – Marc Gravell Aug 21 '13 at 12:27
  • @StingyJack Incorrect, floating point literals are assumed to be doubles unless an `f` is added. OP, See this link: http://msdn.microsoft.com/en-us/library/kfsatb94.aspx – Rotem Aug 21 '13 at 12:27
  • @StingyJack I assume the downvoting is because the question has been asked many times and shows no effort in finding an answer. – Rotem Aug 21 '13 at 12:28
  • @Rotem - just trying to help him work towards an answer instead of getting rabidly downvoted – StingyJack Aug 21 '13 at 12:29
  • @StringyJack: `var rateUsed = 0.00000001; Console.WriteLine(rateUsed.GetType().Name); // Double` – Brad Christie Aug 21 '13 at 12:29
  • @Rotem Can you provide a link? – JMK Aug 21 '13 at 12:29
  • @JMK I did: http://msdn.microsoft.com/en-us/library/kfsatb94.aspx See all the examples at the bottom of the page. – Rotem Aug 21 '13 at 12:29
  • 1
    [This](http://stackoverflow.com/questions/1546113/double-to-string-conversion-without-scientific-notation) and [This](http://stackoverflow.com/questions/14964737/double-tostring-no-scientific-notation) look like the same question – Jonesopolis Aug 21 '13 at 12:30
  • 1
    I guess this is a duplicate, I did actually look, I just wasn't aware of the term 'scientific notation', now I know, cheers guys! – JMK Aug 21 '13 at 12:33
  • @JMK It's also advisable to actually look at documentation before immediately asking question. I don't know what it is with the aversion to reading documentation. If you look at the [MSDN page for Convert.ToString](http://msdn.microsoft.com/en-us/library/system.convert.tostring.aspx) you'd find [ToString(double value, IFormatProvider provider)](http://msdn.microsoft.com/en-us/library/ms131010.aspx) which even demonstrates this exact behavior. – tnw Aug 21 '13 at 12:35

3 Answers3

7

If you want a specific format, then you need to tell it what you want via the parameter. The available standard formats are here, or you can use custom formats. Pass your chosen format to either ToString(format) or Convert.ToString(value, format).

Note; 1E-8 is exactly 0.00000001 - that is simply using exponential notation, i.e. this is 1×10-8

The default format is "general" which is described as:

Result: The most compact of either fixed-point or scientific notation

So yes, sometimes it will use scientific (aka exponential) notation - specifically, it will do so whenever that is shorter than the fixed-point notation. Maybe try using .ToString("F")

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

You may want to start off looking at When does Double.ToString() return a value in scientific notation?

With that said, as @MarcGravell inferred, 1E-08=0.00000001, it's just been displayed in scientific notation. If you're looking for something to output as a decimal number (typical) you may want to look in to formatting it differently. Such as:

var rateUsed = 0.00000001;
var rateUsedToStringFormatted = rateUsed.ToString("0.########"); // 0.00000001

Another option is to create a custom IFormatProvider/ICustomFormatter which would perform this conversion consistently. Then, instead of using .ToString("0.########") everywhere, you could supply .ToString(MyDoubleFormatter) for a more consistent output (and, should you change it at a later date, you're doing so in one location).

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

You probably want to use the fixed-point ("F") format specifier with a different overload of ToString, one which accepts the format specifier string:

int num = 175;
Console.WriteLine(num.ToString("F", CultureInfo.InvariantCulture));  // 175.00
Console.WriteLine(num.ToString("F3", CultureInfo.InvariantCulture)); // 175.000 

Alternatively, you can use a custom format specifier:

Console.WriteLine(num.ToString("00000.00", CultureInfo.InvariantCulture)); // 00175.00

Additionally, as shown in these examples, whenever you are converting values to their text representations, consider using CultureInfo.InvariantCulture for data which should be stored independently of the current thread's culture (like when writing data to a file, for example).

vgru
  • 49,838
  • 16
  • 120
  • 201