Module

Data.Currency

Package
purescript-currency
Repository
philippedev101/purescript-currency

#Money Source

newtype Money :: Symbol -> Typenewtype Money c

An amount of money stored as an integer count of minor currency units (e.g. cents for EUR/USD, yen for JPY).

The phantom type parameter c tracks the currency at the type level, preventing accidental mixing of different currencies.

Instances

#money Source

money :: forall c. Int -> Int -> Int -> Money c

Construct a Money value from major + minor units with an explicit scale. e.g. money 10 50 100 for 10.50 in a 2-decimal currency.

#mkMoney Source

mkMoney :: forall c. CurrencyScale c => Int -> Int -> Money c

Construct a Money value from major + minor units using the currency's known scale. e.g. mkMoney 10 50 :: Money "EUR" for EUR 10.50.

#fromCents Source

fromCents :: forall c. BigInt -> Money c

Construct from a raw count of minor units.

#cents Source

cents :: forall c. Money c -> BigInt

Extract the raw minor unit count.

#fromNumber Source

fromNumber :: forall c. Int -> Number -> Maybe (Money c)

Convert from a Number (representing major units, e.g. 10.50) by multiplying by minorPerMajor and truncating. Returns Nothing if the Number is not finite.

#toNumber Source

toNumber :: forall c. Int -> Money c -> Number

Convert to a Number (major units). Potentially lossy for very large amounts.

#add Source

add :: forall c. Money c -> Money c -> Money c

Add two money amounts.

#subtract Source

subtract :: forall c. Money c -> Money c -> Money c

Subtract: a - b.

#negate Source

negate :: forall c. Money c -> Money c

Negate an amount.

#abs Source

abs :: forall c. Money c -> Money c

Absolute value.

#sign Source

sign :: forall c. Money c -> Int

Returns -1 for negative, 0 for zero, 1 for positive.

#compare Source

compare :: forall c. Money c -> Money c -> Ordering

Compare two money amounts, returning an Ordering.

#isZero Source

isZero :: forall c. Money c -> Boolean

Check if an amount is zero.

#isPositive Source

isPositive :: forall c. Money c -> Boolean

Check if an amount is strictly positive.

#isNegative Source

isNegative :: forall c. Money c -> Boolean

Check if an amount is strictly negative.

#mulInt Source

mulInt :: forall c. Money c -> Int -> Money c

Multiply by an integer scalar.

#mulRate Source

mulRate :: forall c. RoundingMode -> Money c -> Int -> Int -> Money c

Multiply by a rational rate expressed as numerator/denominator. e.g. 19% VAT = mulRate RoundHalfEven m 19 100 This avoids floating point entirely.

#divide Source

divide :: forall c. RoundingMode -> Money c -> Int -> Money c

Divide a money amount by an integer with a rounding mode. Unlike allocate, this does not preserve the total — rounding may cause a small discrepancy. Use allocate or split when the total must be preserved exactly.

#pow Source

pow :: forall c. Money c -> Int -> Money c

Raise a money amount to an integer power. Useful for compound interest calculations.

#allocate Source

allocate :: forall c. Money c -> Array Int -> Array (Money c)

Distribute a Money amount according to a list of ratios. The remainder (due to integer division) is distributed one unit at a time across the parts, starting from the first. The output array has the same length as the ratios array. All ratios must be non-negative; the total of ratios must be positive.

#split Source

split :: forall c. Money c -> Int -> Array (Money c)

Split a Money amount into N equal parts, distributing the remainder.

#minimum Source

minimum :: forall c. Array (Money c) -> Maybe (Money c)

Find the minimum of an array of money amounts.

#maximum Source

maximum :: forall c. Array (Money c) -> Maybe (Money c)

Find the maximum of an array of money amounts.

#haveSameAmount Source

haveSameAmount :: forall c1 c2. Money c1 -> Money c2 -> Boolean

Compare amounts across different currencies (ignoring currency). Returns true if the underlying minor unit counts are equal.

#hasSubUnits Source

hasSubUnits :: forall c. Int -> Money c -> Boolean

Check if the amount has fractional minor units at the given scale. e.g. hasSubUnits 100 (fromCents 1050) is true (10.50 has sub-units), hasSubUnits 100 (fromCents 1000) is false (10.00 has none).

#toFixed Source

toFixed :: forall c. Int -> Money c -> String

Format a Money amount as a decimal string with a specified number of decimal places. e.g. toFixed 2 (fromCents 1050) = "10.50"

#toDecimal Source

toDecimal :: forall c. CurrencyScale c => Money c -> String

Format a Money amount as a decimal string, using the currency's known scale to determine the number of decimal places. e.g. for EUR (scale 100): toDecimal (fromCents 1050) = "10.50" e.g. for JPY (scale 1): toDecimal (fromCents 500) = "500"

#toUnits Source

toUnits :: forall c. Int -> Money c -> Tuple BigInt BigInt

Split a Money amount into major and minor unit parts. Uses Euclidean division, so the minor part is always non-negative. e.g. toUnits 100 (fromCents 1050) = Tuple 10 50

#toJSON Source

toJSON :: forall c. IsSymbol c => Money c -> Tuple String String

Serialize a Money amount to a currency code and amount string pair. e.g. toJSON (fromCents 1050 :: Money "EUR") = Tuple "EUR" "1050"

#fromJSON Source

fromJSON :: forall c. Tuple String String -> Maybe (Money c)

Deserialize a Money amount from a currency code and amount string pair. Returns Nothing if the amount string is not a valid integer. Note: the currency code is not validated against the phantom type — the caller must ensure it matches.

#convert Source

convert :: forall from to. RoundingMode -> Money from -> Int -> Int -> Money to

Convert a Money amount from one currency to another using an exchange rate expressed as numerator/denominator to avoid floating point. e.g. EUR to USD at rate 1.0835: convert RoundHalfEven m 10835 10000 The caller is responsible for adjusting for different minor unit scales between source and target currencies.

#CurrencyScale Source

class CurrencyScale :: Symbol -> Constraintclass CurrencyScale c  where

Type class for currencies with a known minor-unit scale. The scale is the number of minor units per major unit (e.g. 100 for EUR/USD, 1 for JPY, 1000 for BHD).

Members

Instances

Re-exports from Data.Currency.Rounding

#RoundingMode Source

data RoundingMode

Rounding modes for operations that may produce fractional minor units.

Constructors

Instances

#roundDiv Source

roundDiv :: RoundingMode -> BigInt -> BigInt -> BigInt

Integer division with a specified rounding mode. Given a numerator and a divisor, returns the quotient rounded according to the mode.