Search code examples
c#doublestring-interpolationc#-6.0

C# 6 how to format double using interpolated string?


I have used interpolated strings for messages containing string variables like $"{EmployeeName}, {Department}". Now I want to use an interpolated string for showing a formatted double.

Example

var aNumberAsString = aDoubleValue.ToString("0.####");

How can I write it as an interpolated string? Something like $"{aDoubleValue} ...."


Solution

  • You can specify a format string after an expression with a colon (:):

    var aNumberAsString = $"{aDoubleValue:0.####}";