Currency

world.money.currency.Currency
See theCurrency companion trait
object Currency

Provides compile-time access to currency precision (minor unit) information.

This object exposes a match type PrecisionOf that maps currency singleton types to their precision values, enabling compile-time validation and optimisation of monetary calculations.

The precisionOf method provides a total function that returns precision at compile-time when the currency type is known, falling back to runtime lookup for generic cases.

Attributes

Example
import world.money.currency.{Currencies, Currency}
// Compile-time precision for known currencies
val jpyPrecision = Currency.precisionOf[Currencies.JPY.type]  // 0
val kesPrecision = Currency.precisionOf[Currencies.KES.type]  // 2
val omrPrecision = Currency.precisionOf[Currencies.OMR.type]  // 3
// Works with generic currency types at runtime
def showPrecision[C <: Currency](using ValueOf[C]): Int =
 Currency.precisionOf[C]
Companion
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Currency.type

Members list

Type members

Types

type PrecisionOf[C <: Currency] = C match { case JPY.type => Some[0] case KRW.type => Some[0] case BHD.type => Some[3] case KWD.type => Some[3] case OMR.type => Some[3] case TND.type => Some[3] case Any => Option[Int] }

Match type that maps currency singleton types to their precision values.

Match type that maps currency singleton types to their precision values.

This type-level function encodes the number of decimal places (minor units) for each currency. It provides compile-time knowledge of precision for known currency types.

Common precision values:

  • 0: Currencies like JPY (Japanese Yen), KRW (Korean Won)
  • 2: Most currencies including KES, EUR, GBP
  • 3: Currencies like OMR (Omani Riyal), KWD (Kuwaiti Dinar)
  • None: Precious metals and currencies without defined minor units

Type parameters

C

The currency singleton type (e.g., Currencies.KES.type)

Attributes

Value members

Concrete methods

transparent inline def precisionOf[C <: Currency](using currency: ValueOf[C]): Option[Int]

Returns the precision (number of minor units) for a currency type.

Returns the precision (number of minor units) for a currency type.

This method provides compile-time constant folding when the currency type is statically known, while maintaining totality by falling back to runtime lookup for generic currency parameters.

Type parameters

C

The currency type whose precision is requested

Attributes

Returns

The number of decimal places for the currency, or None if not defined

transparent inline def validatePrecision[C <: Currency](value: BigDecimal)(using currency: ValueOf[C]): Boolean

Validates that a BigDecimal value has precision suitable for a currency.

Validates that a BigDecimal value has precision suitable for a currency.

Checks whether the scale (number of decimal places) of the value does not exceed the currency's defined minor unit precision.

Type parameters

C

The currency type

Value parameters

value

The BigDecimal value to validate

Attributes

Returns

true if the value's scale is within the currency's precision, false otherwise

Example
import world.money.currency.{Currencies, Currency}
Currency.validatePrecision[Currencies.JPY.type](BigDecimal("100"))    // true
Currency.validatePrecision[Currencies.JPY.type](BigDecimal("100.5"))  // false
Currency.validatePrecision[Currencies.KES.type](BigDecimal("10.99"))  // true
Currency.validatePrecision[Currencies.KES.type](BigDecimal("10.999")) // false

Givens

Givens

given derived$CanEqual: CanEqual[Currency, Currency]