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

ToSpecificCurrencyString

Convert a double to a string formatted using the culture settings (string representation) passed into the procedure.

Source

/// <summary>
/// Extensions for numeric types.
/// </summary>
public static class NumericExtensions
{
    /// <summary>
    /// Format a double using specific culture currency settings.
    /// </summary>
    /// <param name="value">The double to be formatted.</param>
    /// <param name="cultureName">The string representation for the culture to be used, for instance "en-US" for US English.</param>
    /// <returns>The double formatted based on specific culture currency settings.</returns>
    public static string ToSpecificCurrencyString(this double value, string cultureName)
    {
        CultureInfo currentCulture = new CultureInfo(cultureName);
        return (string.Format(currentCulture, "{0:C}", value));
    }
}

Example

double test = 120.45;
string testString = test.ToSpecificCurrencyString("en-US");

Author: Inge Schepers

Submitted on: 15 dec 2007

Language: C#

Type: System.Double

Views: 2844