C# 6.0 brings this nifty new formatting operation indicated by a $
Instead of doing this
String lastName = "Doena";
String firstName = "DJ";
Console.WriteLine(String.Format("{1} {0}", lastName, firstName));
you can do this
Console.WriteLine($"{firstName} {lastName}");
But what about number formats. What if I have this:
Decimal price = 9999.95m;
Decimal freebie = 0;
const String format = "#,##0.##";
Console.WriteLine(String.Format("{0:" + format + "}\t{1:" + format + "}", price, freebie));
I tried this:
Console.WriteLine($"{price:"{format}"}\t{freebie:"{format}"}");
and this:
Console.WriteLine($"{price:{format}}\t{freebie:{format}}");
and this:
Console.WriteLine($"{price:format}\t{freebie:format}");
But they either not even compile or do not bring the hoped result.
Any ideas?
Edit Howwie's answer seems to be the reasonable way to go here:
Console.WriteLine($"{price.ToString(format)}\t{freebie.ToString(format)}");
Howwie's answer seems to be the reasonable way to go here:
Console.WriteLine($"{price.ToString(format)}\t{freebie.ToString(format)}");