4

As a follow up to a question I had about optimising the conversion of double to string for memory (see c# double to character array or alternative) I wanted to see how double.ToString() is implemented under the hood in .NET. When I use ildasm or DotPeek i get as far as:

[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
public static string FormatDouble(double value, string format, NumberFormatInfo info);

or

IL_0008:  call string System.Number::FormatDouble(float64,
                                                  string,
                                                  class System.Globalization.NumberFormatInfo)

And can't drill further. I think if my understanding is right it's because this is a call into the CLR. I wanted to know is there any easy way to find out what the implementation is?

pogosama
  • 1,818
  • 1
  • 24
  • 29
user555265
  • 493
  • 2
  • 7
  • 18

4 Answers4

3

What about looking at mono source code? Mono simply use IL to do the whole thing without any C code.

mcs/class/corlib/System/Double.cs
mcs/class/corlib/System/NumberFormatter.cs
linquize
  • 19,828
  • 10
  • 59
  • 83
3

Since .NET Core is open source now, answer to this question can be updated. MethodImplOptions.InternalCall indicates FormatDouble is a FCall, which is a mechanism of communicating to native implementation. You can find the native implementation of FormatDouble here.

Jim Ma
  • 709
  • 5
  • 15
1

I think WinDbg would be helpful - and did you checkout the implementation in mono?

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
0

In .NET 5, FormatDouble is now implemented in C#, the source code is available in the dotnet/runtime repository.

pogosama
  • 1,818
  • 1
  • 24
  • 29