Danie
Danie

Reputation: 163

Custom Currency Format in C#

I have a function:

string FormatCurrency(string currencyCode, decimal amount)

Example:

Input: "USD", 1230.56
Output: "USD 1,230.56"

Input: "USD", 1230.00
Output: "USD 1,230"

Input: "VND", 1200000
Output: "1.200.000 VND"

I want to join culture format(with code, not symbols) and custom format ("#,##.##"). How can I implement? Thanks

My code, I got from Internet:

CultureInfo culture = (from c in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                       let r = new RegionInfo(c.LCID)
                       where r != null
                       && r.ISOCurrencySymbol.ToUpper() == currencyCode.ToUpper()
                       select c).FirstOrDefault();

if (culture == null)
{
    return amount.ToString("#,##.##");
}

return string.Format(culture, "{0:C2}", amount);

but the output is "$1,200.00". My expected result is "USD 1,200"

Upvotes: 2

Views: 4682

Answers (3)

Jcl
Jcl

Reputation: 28272

A bit hackish, but you can do something like:

CultureInfo culture = (from c in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
               let r = new RegionInfo(c.LCID)
               where r != null
               && r.ISOCurrencySymbol.ToUpper() == currencyCode.ToUpper()
               select c).FirstOrDefault();
if(culture == null)
{
  // fall back to current culture if none is found
  // you could throw an exception here if that's not supposed to happen
  culture = CultureInfo.CurrentCulture;            
}
culture = (CultureInfo)culture.Clone();
culture.NumberFormat.CurrencySymbol = currencyCode;

// Add spaces between the figure and the currency code
culture.NumberFormat.CurrencyPositivePattern = culture.NumberFormat.CurrencyPositivePattern == 0 ? 2 : 3;
var cnp = culture.NumberFormat.CurrencyNegativePattern;
switch(cnp)
{
  case 0: cnp = 14; break;
  case 1: cnp = 9; break;
  case 2: cnp = 12; break;
  case 3: cnp = 11; break;
  case 4: cnp = 15; break;
  case 5: cnp = 8; break;
  case 6: cnp = 13; break;
  case 7: cnp = 10; break;
}
culture.NumberFormat.CurrencyNegativePattern = cnp;

return amount.ToString("C" + ((amount % 1) == 0?"0":"2"), culture);     

Fiddle here: https://dotnetfiddle.net/30f7u3

For:

FormatCurrency("USD", 1230.56M);
FormatCurrency("USD", 1230.00M);
FormatCurrency("VND", 1200000M);

I get:

USD 1,230.56
USD 1,230
1.200.000 VND

Upvotes: 3

Yasser
Yasser

Reputation: 874

I hope this will help you. If I understand right you want to show everything as the current cultrue, just you want to change how the currency will be shown.

This is the basics and you can continue on your own.

        CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
        culture.NumberFormat.CurrencySymbol = currencyCode;
        culture.NumberFormat.CurrencyPositivePattern = 2;

        return string.Format(culture, "{0:C2}", amount);

The result for me was USD 42,55.

Upvotes: 1

Thorarins
Thorarins

Reputation: 1904

a bit ugly but somthing like this would probably work unless you decide to use culture and currency format

var output = String.Format ("{0} {1:### ### ##0.00}",currencyCode,amount)

for currency format using culture use

var output = amount.ToString ("C");

Upvotes: 0

Related Questions