Table of Contents

MoneyCompactFormattingExtensions Class

Definition

Namespace
Bodu.Financial.Extensions
Assembly
Bodu.Financial.dll
Package
Bodu.Financial 1.0.0
Source
MoneyCompactFormattingExtensions.ToCompactString.cs

Provides compact-notation formatting ("$1.2K", "€1.5M", "USD 2.3B") for Money<TCurrency> and Money values, on top of the standard format-specifier vocabulary.

public static class MoneyCompactFormattingExtensions
Inheritance
MoneyCompactFormattingExtensions
Inherited Members

Remarks

Compact formatting scales the amount by an order-of-magnitude factor (one thousand, one million, one billion, or one trillion) and appends a single-letter magnitude suffix (K, M, B, or T) to the numeric portion of the output. The chosen format specifier (C, G, L, N, F, D) still drives where the currency designator appears, so the suffix attaches to the numeric portion in the correct culture position - for example, "$1.2K" in en-US for USD versus "1,2K €" in fr-FR for EUR.

The R specifier is not compatible with compact formatting because the round-trip form is meant to parse back to the original value; combining the two would silently lose precision. Compact extensions therefore reject the R specifier and the "~" prefix on R.

Magnitude selection is by absolute value: values below 1,000 keep their natural scale, values from 1,000 use K, from 1,000,000 use M, from 1,000,000,000 use B, and from 1,000,000,000,000 use T. Values above the trillion threshold continue to use T (so 5,000,000,000,000 becomes 5T).

Methods

ToCompactString(Money, string?, IFormatProvider?, int)

Returns a compact-notation representation of money with a magnitude suffix (K, M, B, T) applied when the amount is at least one thousand in magnitude.

public static string ToCompactString(this Money money, string? format = "C", IFormatProvider? provider = null, int precision = 1)

Parameters

money Money

The runtime-tagged monetary amount to format.

format string

The format specifier; see ToString(string?, IFormatProvider?) for the supported vocabulary. The R specifier is rejected because compact notation cannot round-trip. Defaults to "C".

provider IFormatProvider

The culture used to render the numeric portion. When omitted, the current culture is used.

precision int

The fractional-digit count to render in the scaled portion. Must be non-negative; defaults to 1.

Returns

string

A compact-notation string such as "$1.2K", "€1.5M", or "USD 2.3B".

Exceptions

ArgumentOutOfRangeException

Thrown when precision is negative.

FormatException

Thrown when format is not a supported specifier, or when it is R (which cannot be combined with compact notation).

ToCompactString<TCurrency>(Money<TCurrency>, string?, IFormatProvider?, int)

Returns a compact-notation representation of money with a magnitude suffix (K, M, B, T) applied when the amount is at least one thousand in magnitude.

public static string ToCompactString<TCurrency>(this Money<TCurrency> money, string? format = "C", IFormatProvider? provider = null, int precision = 1) where TCurrency : ICurrency

Parameters

money Money<TCurrency>

The monetary amount to format.

format string

The format specifier; see ToString(string?, IFormatProvider?) for the supported vocabulary. The R specifier is rejected because compact notation cannot round-trip. Defaults to "C".

provider IFormatProvider

The culture used to render the numeric portion. When omitted, the current culture is used.

precision int

The fractional-digit count to render in the scaled portion. Must be non-negative; defaults to 1.

Returns

string

A compact-notation string such as "$1.2K", "€1.5M", or "USD 2.3B".

Type Parameters

TCurrency

The currency tag type.

Exceptions

ArgumentOutOfRangeException

Thrown when precision is negative.

FormatException

Thrown when format is not a supported specifier, or when it is R (which cannot be combined with compact notation).

Applies to

ProductVersions
.NET8, 10