Search code examples
c#asp.net-corecurrency-formatting

Change the currency symbol placement


I want to know if it's possible to change the default placement of the currency symbol. For example the default output will be something like that : $123,00 but I want it to be like that 123,00 $.

Note that the culture may change with the user settings

public CultureInfo CurrencyCulture { get; }

public string FormatCurrency(decimal value)
{
    var decimalPlace = _config.GetValue<int>("Global.CurrencyDecimalPlace");
    return value.ToString($"C{decimalPlace}", CurrencyCulture);
}

Inside the CSHTML

<tr>
    <td>
        <div>@item.Product.Name</div>
    </td>
    <td>@CurrencyService.FormatCurrency(item.ProductPrice)</td>
    <td>@item.Quantity</td>
    <td>@CurrencyService.FormatCurrency(item.ProductPrice * item.Quantity))</td>
</tr>

Solution

  • The little bit of information that you are missing is the property CurrencyPositivePattern and its more complex counterpart CurrencyNegativePattern

    In the remarks section you could find this table

    Value   Associated pattern
    0       $n
    1       n$
    2       $ n
    3       n $
    

    So your code could be written as

    public string FormatCurrency(decimal value)
    {
        CurrencyCulture.NumberFormat.CurrencyPositivePattern = 3;
        var decimalPlace = _config.GetValue<int>("Global.CurrencyDecimalPlace");
        return value.ToString($"C{decimalPlace}", CurrencyCulture);
    }