The FastMoney type is an optimized version of Money designed for maximum performance in high-throughput scenarios. Unlike Money (which is based on decimal), FastMoney is based on long using 64-bit integer arithmetic with a fixed four-decimal scale.
Key Characteristics:
- Precision: 17 digits (±1.0 × 10^-17 to ±9.2 × 10^17).
- Scale: Fixed four decimal places.
- Performance: Up to 18 times faster for arithmetic operations (add, subtract, multiply, divide, etc.).
- Size: 12 bytes (vs. 16 bytes for
Money). - Compatibility: Aligns with
SqlMoney and OLE Automation Currency (OACurrency).
Note: Use FastMoney only when performance is critical and you can manage the loss of precision and fixed rounding. For presentation, formatting, or rounding based on currency minor units, convert FastMoney to Money using .ToMoney() or an explicit cast.
using NodaMoney;
var eur = new FastMoney(10.1234m, "EUR");
var fee = new FastMoney(0.1000m, "EUR");
var total = eur + fee; // currency-safe operations
// Convert to Money for formatting/rounding/presentation
Money display = eur.ToMoney(); // or (Money)eur
var text = display.ToString("C");
// Convert from Money (explicit)
var money = new Money(12.34m, "EUR");
FastMoney fast = (FastMoney)money; // or new FastMoney(money)
// Convert from SqlMoney
SqlMoney sqlMoney = db.MoneyFromDb();
FastMoney? fast = FastMoney.FromSqlMoney(sqlMoney, Currency.FromCode("EUR")); // or (FastMoney?)sqlMoney
// Convert to SqlMoney
SqlMoney sqlMoney1 = fast.ToSqlMoney();
// Convert from OLE Automation Currency
long oaCurrency = db.CurrencyFromDb();
FastMoney fast = FastMoney.FromOACurrency(oaCurrency, Currency.FromCode("EUR"));
// Convert to OLE Automation Currency
long oaCurrency = fast.ToOACurrency();