Help Needed with a little .Net function...
/// <summary> ///Given an 'ISO 4217' three letter code for a currency... ///Return the 'CurrencySymbol' that represents it /// </summary> /// <param name="ISOCurrency">3 letter ISO427 currency code, e.g. "USD", "GBP", "EUR", "AUD"</param> /// <returns>Currency Symbol, e.g. "$", "£", "€", "$"</returns> public string GetCurrencySymbol(string ISOCurrency) { return "HELP ME!"; }
'Daniel' on Mon, 12 Dec 2005 03:02:48 GMT, sez: Would something like work?:
public string GetCurrencySymbol(string ISOCurrency)
{
RegionInfo ri = new RegionInfo(ISOCurrency)
return ri.ISOCurrencySymbol;
}
'leon' on Mon, 12 Dec 2005 03:38:37 GMT, sez: no sorry daniel -- not quite right.
the constructor to RegionInfo takes a two-letter Region code, such as "IT" for italy.
What i want to send in is a currency indicator, such as "EUR" for Euro.
The last line could instead be
"return ri.CurrencySymbol;"
'Daniel' on Mon, 12 Dec 2005 03:51:37 GMT, sez: Sorry I should have expanded, I was meaning could you change your requirements to fit the RegionInfo class?
'leon' on Mon, 12 Dec 2005 04:00:24 GMT, sez: "could you change your requirements to fit "
I like the way you think Daniel -- though i'd rather not change the requirements.
in any case -- it's great to be visited by a Brisbaner!
'hardbutnot' on Mon, 12 Dec 2005 09:05:49 GMT, sez: As always, first choice solution = google for it, <strike>steal</strike> borrow/pay for it...
http://www.freedownloadmanager.org/downloads/Country_Codes_28720_p/
a free database of country - currency - lots of other rubbish. yay.
but. Then I think that if it was me I'd use the function detailed by Daniel previously and add a Currency to Country convertion bit before calling the "return ri.CurrencySymbol;" line.
So then I went googling for Currency to Country and found the following on MSDN...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/pjsdk/html/pjprocurrencysymbol_HV45317832.asp
-----------8<----------
Select Case Country
Case "US", "United States", "USA", "United States of America"
ActiveProject.CurrencySymbol = "$"
ActiveProject.CurrencySymbolPosition = pjBefore
Case "ENGLAND"
ActiveProject.CurrencySymbol = Chr(163)
ActiveProject.CurrencySymbolPosition = pjBefore
Case "SWEDEN"
ActiveProject.CurrencySymbol = "kr"
ActiveProject.CurrencySymbolPosition = pjAfterWithSpace
' Warn user if the currency format is not known.
Case Else
MsgBox ("The currency format for that country is unknown.")
End Select
-----------8<----------
which made me laugh. Three ways to specify USA, ENGLAND (not britain or uk or scotland or wales or ireland or ...) and SWEDEN listed as if they are the only other two countries in the world. Else *Unknown*.
So I suggest copying this logic (if it's good enough for MS...)
public string GetCurrencySymbol(string ISOCurrency) {
if (ISOCurrency == 'USD') return '$';
elseif (ISOCurrency == 'GBP') return '£';
elseif (ISOCurrency == 'SEK') return 'K';
else return '';
}
and final solution -- do some javascript to pull the information from this page !
http://www.xe.com/iso4217.htm
'Omer van Kloeten' on Mon, 12 Dec 2005 16:27:49 GMT, sez:
//Don't forget: 'using System.Globalization;'
private static Hashtable currencySymbols;
/// <summary>
/// Given an 'ISO 4217' three letter code for a currency...
/// Return the 'CurrencySymbol' that represents it
/// </summary>
/// <param name="ISOCurrency">3 letter ISO427 currency code, e.g. 'USD', 'GBP', 'EUR', 'AUD'</param>
/// <returns>Currency Symbol, e.g. '$', '£', '€', '$'</returns>
public static string GetCurrencySymbol(string ISOCurrency)
{
if (currencySymbols == null)
{
currencySymbols = new Hashtable();
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
{
RegionInfo ri = new RegionInfo(ci.LCID);
if(!currencySymbols.ContainsKey(ri.ISOCurrencySymbol))
currencySymbols.Add(ri.ISOCurrencySymbol, ri.CurrencySymbol);
}
}
return currencySymbols[ISOCurrency].ToString();
}
I think it's the only way. Ugly, but I think it's the only way.
Also, consider making the method static. It's not really an instance action, IMHO.
'leon' on Mon, 12 Dec 2005 20:26:05 GMT, sez: Thanks Omer!
Brilliant solution.
(i re-formatted it as html, using the "Copy as Html" add in for Visual Studio)
Anyway: I'm not going to use it... because on closer inspection what the client would like is if we don't use a currency symbol at all for foreign currencies -- just include a label that says "All prices are in GBP"...
'=8)-DX' on Fri, 09 Jun 2006 21:18:50 GMT, sez: I've just been working on this problem - it is much simpler, Daniel almost got it. The catch is that the first two letters of the ISO 4217 Currency codes are the region (Ie US in USD)
public string GetCurrencySymbol(string ISOCurrency)
{
string ISORegion = ISOCurrency.SubString(0,2)
RegionInfo ri = new RegionInfo(ISORegion)
return ri.ISOCurrencySymbol;
}
|