Class CurrencyService
- Namespace
- Dynamicweb.Ecommerce.International
- Assembly
- Dynamicweb.Ecommerce.dll
Currency service provides operations for working with the currency model
public class CurrencyService : ICacheStorage<string, Currency>, ICacheStorage<string>, ICacheStorage
- Inheritance
-
CurrencyService
- Implements
- Inherited Members
Methods
CleanDefault()
Remove default property from currencies.
public virtual void CleanDefault()
ClearCache()
Resets all keys to the default value for objects stored in the object cache
public void ClearCache()
ClearCache(IEnumerable<string>)
Resets the specified keys to the default value for objects stored in the object cache
public void ClearCache(IEnumerable<string> keys)
Parameters
keys
IEnumerable<string>Keys to reset
ClearCache(string)
Resets the specified key to the default value for objects stored in the object cache
public void ClearCache(string key)
Parameters
key
stringKey to reset
Delete(string)
Deletes an instance with the specified parameters.
public virtual void Delete(string currencyCode)
Parameters
currencyCode
stringThe currency code.
Format(Currency, double)
Formats the specified value according to the currency settings.
public virtual string Format(Currency currency, double value)
Parameters
currency
CurrencyThe currency to use for formatting the value
value
doubleThe value - the price to format to a string.
Returns
- string
The formatted value.
Format(Currency, double, bool)
Formats the specified value according to the currency settings.
public virtual string Format(Currency currency, double value, bool showSymbol)
Parameters
currency
CurrencyThe currency to use for formatting the value.
value
doubleThe value - the price to format to a string.
showSymbol
boolIf set to
true
, includes the currency symbol.
Returns
- string
The formatted value as a string.
Examples
var currencyService = new CurrencyService();
var currency = currencyService.GetCurrency("USD");
string formattedValue = currencyService.Format(currency, 1234.56, true);
// formattedValue might be "$1,234.56" depending on the currency settings
Remarks
This method formats a given value according to the specified currency's settings. It takes into account the currency's culture info, rounding settings, and whether to show the currency symbol. If the currency's culture info is specified and contains a hyphen, it uses the NumberFormatInfo from the cache. Otherwise, it falls back to the FormatCurrency(Currency, double, bool) method.
FormatCurrency(Currency, double, bool)
Formats the currency using the currency rounding method.
public virtual string FormatCurrency(Currency currency, double value, bool showSymbol)
Parameters
currency
CurrencyThe currency to use for formatting the value
value
doubleThe value to format to a currency.
showSymbol
boolif set to
true
show the currency symbol, i.e. $, £, € or DKK.
Returns
- string
System.String., i.e. $1,000.23 or DKK 1.000,23 or €1.000,23
FormatCurrency(Currency, double, bool, bool)
Formats the currency to a currency string. Rounding is either handled by the currency rounding or to nearest 2 decimals.
public virtual string FormatCurrency(Currency currency, double value, bool showSymbol, bool useCurrencyRounding)
Parameters
currency
CurrencyThe currency to use for formatting the value
value
doubleThe value to format to a currency.
showSymbol
boolif set to
true
show the currency symbol, i.e. $, £, € or DKK.useCurrencyRounding
boolif set to
true
the rounding specified on the currency will be used for rounding, otherwise the value is rounded to 2 decimals.
Returns
- string
System.String., i.e. $1,000.23 or DKK 1.000,23 or €1.000,23
GetAllCurrencies()
Gets all currencies.
public virtual IEnumerable<Currency> GetAllCurrencies()
Returns
- IEnumerable<Currency>
A collection of the Currency.
GetCurrency(string)
Gets a currency by currency code.
public virtual Currency GetCurrency(string currencyCode)
Parameters
currencyCode
stringThe currency code.
Returns
GetCurrencyOrDefault(string)
Gets the currency by currency code. Returns the default currency if the selected currency is not found.
public virtual Currency GetCurrencyOrDefault(string currencyCode)
Parameters
currencyCode
stringCurrency code
Returns
GetDefaultCurrency()
Gets the default currency.
public virtual Currency GetDefaultCurrency()
Returns
GetNumberFormatInfo(Currency)
Returns number format info related to specified currency. If currency's culture info is not found, get number format info from execution context.
public virtual NumberFormatInfo GetNumberFormatInfo(Currency currency)
Parameters
currency
CurrencyThe currency to provide number format info.
Returns
- NumberFormatInfo
The NumberFormatInfo object.
GetOrCreateCurrency(string, string, double, string)
Gets existed currency. If currency is not found, creates the custom currency based on currency data.
public virtual Currency GetOrCreateCurrency(string currencyCode, string currencyName, double currencyRate, string languageId)
Parameters
currencyCode
stringCurrency code
currencyName
stringCurrency name
currencyRate
doubleCurrency rate
languageId
stringLanguage id
Returns
- Currency
Returns the currency by currency code. If currency code is empty, returns null. If currency code is set, but nothing is found, returns custom currency.
Round(Currency, double)
Rounds the value.
public virtual double Round(Currency currency, double value)
Parameters
Returns
- double
The rounded value.
Save(Currency)
Saves a currency instance
public virtual void Save(Currency currency)
Parameters
currency
CurrencyThe currency.
UpdateCurrencyCode(string, string)
Update currency and its relations with a new Code.
public virtual void UpdateCurrencyCode(string oldCode, string newCode)