ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToLocalCurrencyString

Convert a double to a string formatted to the specified CultureInfo or the Default CultureInfo if null.

Source

/// <summary>
/// Format a double using the local culture currency settings.
/// </summary>
/// <param name="value">The double to be formatted.</param>
/// <param name="culture">(Optional) If specified, uses the specified cultures currency format. Otherwise uses machines current culture.</param>
/// <returns>The double formatted based on the local culture currency settings.</returns>
public static string ToLocalCurrencyString(this double value, CultureInfo? culture = null)
{
    culture ??= CultureInfo.CurrentCulture;

    return value.ToString("c", culture);
}

Example

double cost = 100;
Console.WriteLine(cost.ToLocalCurrencyString()) 'returns $100.00
Console.WriteLine(cost.ToLocalCurrencyString(), new CultureInfo("da-DK")) 'returns 100,00 kr.

Author: John Tolar

Submitted on: 26 mrt. 2022

Language: csharp

Type: System.Double

Views: 2798