C# 6 how to format double using interpolated string?

C#DoubleString InterpolationC# 6.0

C# Problem Overview


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} ...."

C# Solutions


Solution 1 - C#

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

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

Solution 2 - C#

A colon after the variable specifies a format,

Console.Write($"{aDoubleValue:0.####}");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMagBView Question on Stackoverflow
Solution 1 - C#lc.View Answer on Stackoverflow
Solution 2 - C#Ash BurlaczenkoView Answer on Stackoverflow