Created
January 31, 2017 04:27
-
-
Save code-atom/da19eea04dce574bc0cab558362732a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Net; | |
using System.Text.RegularExpressions; | |
using ClassLibraries.SiteSettings.Classes; | |
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Text; | |
namespace ClassLibraries.SiteSettings.Currency | |
{ | |
public static class CurrencyTool | |
{ | |
public static bool TryGetCurrencySymbol(string ISOCurrencySymbol, out RegionInfo symbol) | |
{ | |
symbol = CultureInfo | |
.GetCultures(CultureTypes.AllCultures) | |
.Where(c => !c.IsNeutralCulture) | |
.Select(culture => | |
{ | |
try | |
{ | |
return new RegionInfo(culture.LCID); | |
} | |
catch | |
{ | |
return null; | |
} | |
}).FirstOrDefault(ri => ri != null && ri.ISOCurrencySymbol == ISOCurrencySymbol); | |
return symbol != null; | |
} | |
public static string GetPriceWithCurrency(this decimal price) | |
{ | |
SiteSetting currencySetting = null; | |
currencySetting = SiteSettingDao.GetByName("Currency"); | |
if (currencySetting != null) | |
{ | |
RegionInfo symbol = null; | |
TryGetCurrencySymbol(currencySetting.Value, out symbol); | |
if (symbol != null) | |
return String.Format("{0} {1} {2}", symbol.CurrencySymbol, price, symbol.ISOCurrencySymbol); | |
} | |
return String.Format("{0} {1} AUD", "$", price); | |
} | |
public static Currency.Classes.Currency GetCurrency() | |
{ | |
SiteSetting currencySetting = null; | |
var currency = new Classes.Currency() | |
{ | |
Code = "$", | |
Name = "AUD", | |
}; | |
currencySetting = SiteSettingDao.GetByName("Currency"); | |
if (currencySetting != null) | |
{ | |
RegionInfo symbol = null; | |
TryGetCurrencySymbol(currencySetting.Value, out symbol); | |
if (symbol != null) | |
{ | |
currency.Code = symbol.CurrencySymbol; | |
currency.Name = symbol.ISOCurrencySymbol; | |
} | |
return currency; | |
} | |
return currency; | |
} | |
public static decimal ConvertCurrency(string fromCurrency, string toCurrency, decimal amount) | |
{ | |
if(amount ==0 ) | |
return (decimal)0.0; | |
//Grab your values and build your Web Request to the API | |
string apiURL = String.Format("https://www.google.com/finance/converter?a={0}&from={1}&to={2}&meta={3}", amount, fromCurrency, toCurrency, Guid.NewGuid().ToString()); | |
//Make your Web Request and grab the results | |
var request = WebRequest.Create(apiURL); | |
//Get the Response | |
var streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII); | |
//Grab your converted value (ie 2.45 USD) | |
var result = Regex.Matches(streamReader.ReadToEnd(), "<span class=\"?bld\"?>([^<]+)</span>")[0].Groups[1].Value; | |
Regex regex = new Regex(@"^-?\d+(?:\.\d+)?"); | |
Match match = regex.Match(result); | |
if (match.Success) | |
{ | |
return Math.Round(decimal.Parse(match.Value), 2); ; | |
} | |
return (decimal) 0.0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment