Class CurrencyExtensions
- Namespace
- HeroKit.Globalization
- Assembly
- HeroKit.dll
Formats amounts and writes them in words with the notation of a Currency.
public static class CurrencyExtensions
- Inheritance
-
CurrencyExtensions
- Inherited Members
Methods
ToAmount(string?, Currency, decimal)
Reads an amount written with the separators of a currency, or returns a default value. See TryToAmount(string?, Currency, out decimal).
public static decimal ToAmount(this string? value, Currency currency, decimal defaultValue = 0)
Parameters
valuestringThe text, for example "₺1.234,56".
currencyCurrencyThe currency whose separators, symbol, abbreviation and ISO code are accepted.
defaultValuedecimalThe value returned when the text is not a valid amount.
Returns
- decimal
The amount, or
defaultValue.
ToCurrencyString(decimal, Currency, CurrencyStyle)
Formats an amount with two decimals in the notation of a currency.
public static string ToCurrencyString(this decimal value, Currency currency, CurrencyStyle style = CurrencyStyle.Symbol)
Parameters
valuedecimalThe amount. It is rounded to two decimals with AwayFromZero.
currencyCurrencyThe currency, for example TurkishLira.
styleCurrencyStyleSymbol before ("₺1.234,50"), abbreviation after ("1.234,50 TL") or ISO code after ("1.234,50 TRY").
Returns
- string
The formatted amount. Negative amounts start with "-".
Examples
1234.5m.ToCurrencyString(Currencies.TurkishLira); // "₺1.234,50"
1234.5m.ToCurrencyString(Currencies.TurkishLira, CurrencyStyle.Abbreviation); // "1.234,50 TL"
ToCurrencyWords(decimal, Currency)
Writes an amount in words with the unit and subunit names of a currency, as printed on invoices.
public static string ToCurrencyWords(this decimal amount, Currency currency)
Parameters
amountdecimalThe amount. It is rounded to two decimals with AwayFromZero.
currencyCurrencyThe currency. Its Language spells the numbers.
Returns
- string
The amount in words, for example "yüz yirmi üç Türk lirası elli kuruş". The subunit part is omitted when zero; the unit part is omitted when zero and the subunit is not.
Examples
123.50m.ToCurrencyWords(Currencies.TurkishLira); // "yüz yirmi üç Türk lirası elli kuruş"
123.50m.ToCurrencyWords(Currencies.USDollar); // "one hundred twenty-three dollars and fifty cents"
TryToAmount(string?, Currency, out decimal)
Reads an amount written with the separators of a currency, with or without its symbol, abbreviation or ISO code.
public static bool TryToAmount(this string? value, Currency currency, out decimal result)
Parameters
valuestringThe text, for example "₺1.234,56", "1.234,56 TL", "-1234,5" or "$1,234.56".
currencyCurrencyThe currency whose separators, symbol, abbreviation and ISO code are accepted.
resultdecimalThe amount when the method returns true; otherwise 0.
Returns
- bool
True if the text is a valid amount.
Examples
"₺1.234,56".TryToAmount(Currencies.TurkishLira, out decimal amount); // true, 1234.56
"1.234".TryToAmount(Currencies.TurkishLira, out _); // true, 1234
"1.5".TryToAmount(Currencies.TurkishLira, out _); // false
Remarks
Group separators must split the whole part into groups of three digits, so "1.234,56" is a Turkish lira amount but "1.5" is rejected instead of being read as 15. The method does not use the culture of the machine.