Table of Contents

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

value string

The text, for example "₺1.234,56".

currency Currency

The currency whose separators, symbol, abbreviation and ISO code are accepted.

defaultValue decimal

The 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

value decimal

The amount. It is rounded to two decimals with AwayFromZero.

currency Currency

The currency, for example TurkishLira.

style CurrencyStyle

Symbol 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

amount decimal

The amount. It is rounded to two decimals with AwayFromZero.

currency Currency

The 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

value string

The text, for example "₺1.234,56", "1.234,56 TL", "-1234,5" or "$1,234.56".

currency Currency

The currency whose separators, symbol, abbreviation and ISO code are accepted.

result decimal

The 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.