简体   繁体   中英

How to get format of currency using current culture information and validate that currency

I have requirement where i have currency code and using that code i am finding the current culture of that currency and also symbol of that currency.

在此处输入图片说明 Here I have the ability to change the currency ISO code and if user changes the code according to that i am changing the currency symbol.

Now i want to find the format of that culture using currency ISO code.Which format that culture uses for currency.How can i find that?

And using that format i want to do the validation of currency.If user enter currency in wrong format then i want to give the validation how can i do that?

The decimal type, which is good for a currency, has the TryParse method that accepts the culture.

bool IsValidCurrency(string s, string tla, out value)
{
  var culture = CultureInfo
   .GetCultures(CultureTypes.AllCultures)
   .Where(c => c.ISOCurrencySymbol == tla)
   .First();
  return decimal.TryParse(s, NumberStyles.Any, culture, out value)
}

Where s is the text representation of the currency value and tla is the ISO three letter abbreviation of the currency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM