Ivandro Jao
Ivandro Jao

Reputation: 2961

How to perform currency formatting with string interpolation in c#?

Is it possible to perform string formatting using c#'s string interpolation with a specific CultureInfo?

For example if you are in a en-US System and want the number to be formatted with pt-PT how to I achieve that with string interpolation?

Upvotes: 0

Views: 2765

Answers (2)

Sweeper
Sweeper

Reputation: 273565

Interpolated strings can be converted to FormattableStrings, which can be formatted with a IFormatProvider:

var formattableString = (FormattableString)$"{1.1:C}";
var result = formattableString.ToString(new CultureInfo("pt-PT"));
// "1,10 €"

Upvotes: 3

devsmn
devsmn

Reputation: 1040

You can use CultureInfo.CreateSpecificCulture for specifying the culture and the C formatter for converting the value into a string with a currency.

double amount = 50.5;
string str = $"{amount.ToString("C", CultureInfo.CreateSpecificCulture("pt-PT"))}";

Upvotes: 3

Related Questions