'How to set a number of decimals in string.Join? [closed]

Console.WriteLine($"{name} -> {string.Join(" ", b[name]):F2} (avg: {b[name].Average():F2})");

I am not exactly sure how to use :F2 in string.Join, since it isn't working. Can you tell me how exactly to use it?



Solution 1:[1]

Assuming that bs indexer returns some collection of numbers - you need to provide correct format before joining numbers into string. You can use ToString overload that accepts the number format: string.Join(" ", b[name].Select(n => n.ToString("F2"))) resulting in something like this:

Console.WriteLine($"{name} -> {string.Join(" ", b[name].Select(n => n.ToString("F2")))} (avg: {b[name].Average():F2})");  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1