hledger-lib-1.33: A library providing the core functionality of hledger
Safe HaskellSafe-Inferred
LanguageHaskell2010

Hledger.Data.Amount

Description

A simple Amount is some quantity of money, shares, or anything else. It has a (possibly null) CommoditySymbol and a numeric quantity:

  $1
  £-50
  EUR 3.44
  GOOG 500
  1.5h
  90 apples
  0

It may also have an AmountCost, representing this amount's per-unit or total cost in a different commodity. If present, this is rendered like so:

  EUR 2 @ $1.50  (unit cost)
  EUR 2 @@ $3   (total cost)

A MixedAmount is zero or more simple amounts, so can represent multiple commodities; this is the type most often used:

  0
  $50 + EUR 3
  16h + $13.55 + AAPL 500 + 6 oranges

A mixed amount is always "normalised", it has no more than one amount in each commodity and cost. When calling amounts it will have no zero amounts, or just a single zero amount and no other amounts.

Limited arithmetic with simple and mixed amounts is supported, best used with similar amounts since it mostly ignores costss and commodity exchange rates.

Synopsis

Commodity

showCommoditySymbol :: Text -> Text Source #

Show space-containing commodity symbols quoted, as they are in a journal.

Amount

arithmetic

nullamt :: Amount Source #

The empty simple amount - a zero with no commodity symbol or cost and the default amount display style.

missingamt :: Amount Source #

A special amount used as a marker, meaning "no explicit amount provided here, infer it when needed". It is nullamt with commodity symbol AUTO.

amountWithCommodity :: CommoditySymbol -> Amount -> Amount Source #

Convert an amount to the specified commodity, ignoring and discarding any costs and assuming an exchange rate of 1.

amountCost :: Amount -> Amount Source #

Convert a amount to its total cost in another commodity, using its attached cost amount if it has one. Notes:

  • cost amounts must be MixedAmounts with exactly one component Amount (or there will be a runtime error XXX)
  • cost amounts should be positive in the Journal (though this is currently not enforced)

amountIsZero :: Amount -> Bool Source #

Is this Amount (and its total cost, if it has one) exactly zero, ignoring its display precision ?

amountLooksZero :: Amount -> Bool Source #

Do this Amount and (and its total cost, if it has one) appear to be zero when rendered with its display precision ? The display precision should usually have a specific value here; if unset, it will be treated like NaturalPrecision.

divideAmount :: Quantity -> Amount -> Amount Source #

Divide an amount's quantity (and total cost, if any) by some number.

multiplyAmount :: Quantity -> Amount -> Amount Source #

Multiply an amount's quantity (and its total cost, if it has one) by a constant.

invertAmount :: Amount -> Amount Source #

Invert an amount (replace its quantity q with 1/q). (Its cost if any is not changed, currently.)

styles

amountstyle :: AmountStyle Source #

Default amount style

canonicaliseAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount Source #

Deprecated: please use styleAmounts instead

styleAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount Source #

Deprecated: please use styleAmounts instead

amountSetStyles :: Map CommoditySymbol AmountStyle -> Amount -> Amount Source #

Deprecated: please use styleAmounts instead

amountStyleSetRounding :: Rounding -> AmountStyle -> AmountStyle Source #

Set this amount style's rounding strategy when it is being applied to amounts.

amountStylesSetRounding :: Rounding -> Map CommoditySymbol AmountStyle -> Map CommoditySymbol AmountStyle Source #

Set these amount styles' rounding strategy when they are being applied to amounts.

amountUnstyled :: Amount -> Amount Source #

Reset this amount's display style to the default.

rendering

data AmountFormat Source #

Formatting options available when displaying Amounts and MixedAmounts. Similar to AmountStyle but lower level, not attached to amounts or commodities, and can override it in some ways. See also hledger manual > "Amount formatting, parseability", which speaks of human, hledger, and machine output.

Constructors

AmountFormat 

Fields

Instances

Instances details
Show AmountFormat Source # 
Instance details

Defined in Hledger.Data.Amount

Default AmountFormat Source #

By default, display amounts using defaultFmt amount display options.

Instance details

Defined in Hledger.Data.Amount

Methods

def :: AmountFormat #

defaultFmt :: AmountFormat Source #

Display amounts without colour, and with various other defaults.

fullZeroFmt :: AmountFormat Source #

Like defaultFmt but show zero amounts with commodity symbol and styling, like non-zero amounts.

noCostFmt :: AmountFormat Source #

Like defaultFmt but don't show costs.

oneLineFmt :: AmountFormat Source #

Like defaultFmt but display all amounts on one line.

oneLineNoCostFmt :: AmountFormat Source #

Like noCostFmt but display all amounts on one line.

machineFmt :: AmountFormat Source #

A (slightly more) machine-readable amount format; like oneLineNoCostFmt but don't show digit group marks.

showAmount :: Amount -> String Source #

Render an amount using its display style and the default amount format. Zero-equivalent amounts are shown as just "0". The special "missing" amount is shown as the empty string.

showAmountWith :: AmountFormat -> Amount -> String Source #

Like showAmount but uses the given amount format.

showAmountB :: AmountFormat -> Amount -> WideBuilder Source #

Render an amount using its display style and the given amount format, as a builder for efficiency. (This can be converted to a Text with wbToText or to a String with wbUnpack). The special "missing" amount is displayed as the empty string.

cshowAmount :: Amount -> String Source #

Colour version. For a negative amount, adds ANSI codes to change the colour, currently to hard-coded red.

cshowAmount = wbUnpack . showAmountB def{displayColour=True}

showAmountWithZeroCommodity :: Amount -> String Source #

Like showAmount, but show a zero amount's commodity if it has one.

showAmountWithZeroCommodity = wbUnpack . showAmountB defaultFmt{displayZeryCommodity=True}

showAmountDebug :: Amount -> String Source #

Get a string representation of an amount for debugging, appropriate to the current debug level. 9 shows maximum detail.

showAmountWithoutCost :: Amount -> String Source #

Get the string representation of an amount, without any @ cost.

showAmountWithoutCost = wbUnpack . showAmountB noCostFmt

amountSetPrecision :: AmountPrecision -> Amount -> Amount Source #

Set an amount's display precision.

amountSetPrecisionMin :: Word8 -> Amount -> Amount Source #

Ensure an amount's display precision is at least the given minimum precision. Always sets an explicit Precision.

amountSetPrecisionMax :: Word8 -> Amount -> Amount Source #

Ensure an amount's display precision is at most the given maximum precision. Always sets an explicit Precision.

withPrecision :: Amount -> AmountPrecision -> Amount Source #

Set an amount's display precision, flipped.

amountSetFullPrecision :: Amount -> Amount Source #

Increase an amount's display precision, if needed, to enough decimal places to show it exactly (showing all significant decimal digits, without trailing zeros). If the amount's display precision is unset, it will be treated as precision 0.

amountSetFullPrecisionUpTo :: Maybe Word8 -> Amount -> Amount Source #

We often want to display "infinite decimal" amounts rounded to some readable number of digits, while still displaying amounts with a large but "non infinite" number of decimal digits (eg 10 or 100 or 200 digits) in full. This helper is like amountSetFullPrecision, but with some refinements:

  1. A maximum display precision can be specified, setting a hard upper limit.
  2. If no limit is specified, and the internal precision is the maximum (255), indicating an infinite decimal, display precision is set to a smaller default (8).

This function always sets an explicit display precision (ie, Precision n).

amountInternalPrecision :: Amount -> Word8 Source #

How many internal decimal digits are stored for this amount ?

amountDisplayPrecision :: Amount -> Word8 Source #

How many decimal digits will be displayed for this amount ?

defaultMaxPrecision :: Word8 Source #

The fallback display precision used when showing amounts representing an infinite decimal.

setAmountInternalPrecision :: Word8 -> Amount -> Amount Source #

Set an amount's internal decimal precision as well as its display precision. This rounds or pads its Decimal quantity to the specified number of decimal places. Rounding is done with Data.Decimal's default roundTo function: "If the value ends in 5 then it is rounded to the nearest even value (Banker's Rounding)".

withInternalPrecision :: Amount -> Word8 -> Amount Source #

setAmountInternalPrecision with arguments flipped.

setAmountDecimalPoint :: Maybe Char -> Amount -> Amount Source #

Set (or clear) an amount's display decimal point.

withDecimalPoint :: Amount -> Maybe Char -> Amount Source #

Set (or clear) an amount's display decimal point, flipped.

amountStripCost :: Amount -> Amount Source #

Strip all costs from an Amount

MixedAmount

nullmixedamt :: MixedAmount Source #

The empty mixed amount.

missingmixedamt :: MixedAmount Source #

A special mixed amount used as a marker, meaning "no explicit amount provided here, infer it when needed".

isMissingMixedAmount :: MixedAmount -> Bool Source #

Does this MixedAmount include the "missing amount" marker ? Note: currently does not test for equality with missingmixedamt, instead it looks for missingamt among the Amounts. missingamt should always be alone, but detect it even if not.

mixed :: Foldable t => t Amount -> MixedAmount Source #

Convert amounts in various commodities into a mixed amount.

mixedAmount :: Amount -> MixedAmount Source #

Create a MixedAmount from a single Amount.

maAddAmount :: MixedAmount -> Amount -> MixedAmount Source #

Add an Amount to a MixedAmount, normalising the result. Amounts with different costs are kept separate.

maAddAmounts :: Foldable t => MixedAmount -> t Amount -> MixedAmount Source #

Add a collection of Amounts to a MixedAmount, normalising the result. Amounts with different costs are kept separate.

amounts :: MixedAmount -> [Amount] Source #

Get a mixed amount's component amounts, with some cleanups. The following descriptions are old and possibly wrong:

  • amounts in the same commodity are combined unless they have different costs or total costs
  • multiple zero amounts, all with the same non-null commodity, are replaced by just the last of them, preserving the commodity and amount style (all but the last zero amount are discarded)
  • multiple zero amounts with multiple commodities, or no commodities, are replaced by one commodity-less zero amount
  • an empty amount list is replaced by one commodity-less zero amount
  • the special "missing" mixed amount remains unchanged

amountsRaw :: MixedAmount -> [Amount] Source #

Get a mixed amount's component amounts without normalising zero and missing amounts. This is used for JSON serialisation, so the order is important. In particular, we want the Amounts given in the order of the MixedAmountKeys, i.e. lexicographically first by commodity, then by cost commodity, then by unit cost from most negative to most positive.

amountsPreservingZeros :: MixedAmount -> [Amount] Source #

Get a mixed amount's component amounts, with some cleanups. This is a new version of amounts, with updated descriptions and optimised for print to show commodityful zeros.

  • If it contains the "missing amount" marker, only that is returned (discarding any additional amounts).
  • Or if it contains any non-zero amounts, only those are returned (discarding any zeroes).
  • Or if it contains any zero amounts (possibly more than one, possibly in different commodities), all of those are returned.
  • Otherwise the null amount is returned.

maCommodities :: MixedAmount -> Set CommoditySymbol Source #

Get this mixed amount's commodities as a set. Returns an empty set if there are no amounts.

filterMixedAmount :: (Amount -> Bool) -> MixedAmount -> MixedAmount Source #

Filter a mixed amount's component amounts by a predicate.

filterMixedAmountByCommodity :: CommoditySymbol -> MixedAmount -> MixedAmount Source #

Return an unnormalised MixedAmount containing just the amounts in the requested commodity from the original mixed amount.

The result will contain at least one Amount of the requested commodity, even if the original mixed amount did not (with quantity zero in that case, and this would be discarded when the mixed amount is next normalised).

The result can contain more than one Amount of the requested commodity, eg because there were several with different costs, or simply because the original mixed amount was was unnormalised.

mapMixedAmount :: (Amount -> Amount) -> MixedAmount -> MixedAmount Source #

Apply a transform to a mixed amount's component Amounts.

unifyMixedAmount :: MixedAmount -> Maybe Amount Source #

Unify a MixedAmount to a single commodity value if possible. This consolidates amounts of the same commodity and discards zero amounts; but this one insists on simplifying to a single commodity, and will return Nothing if this is not possible.

mixedAmountStripCosts :: MixedAmount -> MixedAmount Source #

Remove all costs from a MixedAmount.

arithmetic

mixedAmountCost :: MixedAmount -> MixedAmount Source #

Convert all component amounts to cost where possible (see amountCost).

maNegate :: MixedAmount -> MixedAmount Source #

Negate mixed amount's quantities (and total costs, if any).

maPlus :: MixedAmount -> MixedAmount -> MixedAmount Source #

Sum two MixedAmount, keeping the cost of the first if any. Amounts with different costs are kept separate (since 2021).

maMinus :: MixedAmount -> MixedAmount -> MixedAmount Source #

Subtract a MixedAmount from another. Amounts with different costs are kept separate.

maSum :: Foldable t => t MixedAmount -> MixedAmount Source #

Sum a collection of MixedAmounts. Amounts with different costs are kept separate.

divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount Source #

Divide a mixed amount's quantities (and total costs, if any) by a constant.

multiplyMixedAmount :: Quantity -> MixedAmount -> MixedAmount Source #

Multiply a mixed amount's quantities (and total costs, if any) by a constant.

averageMixedAmounts :: [MixedAmount] -> MixedAmount Source #

Calculate the average of some mixed amounts.

isNegativeAmount :: Amount -> Bool Source #

Is this amount negative ? The cost is ignored.

isNegativeMixedAmount :: MixedAmount -> Maybe Bool Source #

Is this mixed amount negative, if we can tell that unambiguously? Ie when normalised, are all individual commodity amounts negative ?

mixedAmountIsZero :: MixedAmount -> Bool Source #

Is this mixed amount exactly zero, ignoring its display precision? See amountIsZero.

maIsZero :: MixedAmount -> Bool Source #

Is this mixed amount exactly zero, ignoring its display precision?

A convenient alias for mixedAmountIsZero.

maIsNonZero :: MixedAmount -> Bool Source #

Is this mixed amount non-zero, ignoring its display precision?

A convenient alias for not . mixedAmountIsZero.

mixedAmountLooksZero :: MixedAmount -> Bool Source #

Does this mixed amount appear to be zero when rendered with its display precision? See amountLooksZero.

styles

canonicaliseMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount Source #

Deprecated: please use mixedAmountSetStyle False (or styleAmounts) instead

styleMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount Source #

Deprecated: please use styleAmounts instead

Given a map of standard commodity display styles, find and apply the appropriate style to each individual amount.

mixedAmountSetStyles :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount Source #

Deprecated: please use styleAmounts instead

mixedAmountUnstyled :: MixedAmount -> MixedAmount Source #

Reset each individual amount's display style to the default.

rendering

showMixedAmount :: MixedAmount -> String Source #

Render a mixed amount using its amount display styles and the default amount format, after normalising it (to at most one amount in each of its commodities). See showMixedAmountB for special cases.

showMixedAmountWith :: AmountFormat -> MixedAmount -> String Source #

Like showMixedAmount but uses the given amount format. See showMixedAmountB for special cases.

showMixedAmountOneLine :: MixedAmount -> String Source #

Get the one-line string representation of a mixed amount (also showing any costs). See showMixedAmountB for special cases.

showMixedAmountDebug :: MixedAmount -> String Source #

Get an unambiguous string representation of a mixed amount for debugging.

showMixedAmountWithoutCost :: Bool -> MixedAmount -> String Source #

Get the string representation of a mixed amount, without showing any costs. With a True argument, adds ANSI codes to show negative amounts in red. See showMixedAmountB for special cases.

showMixedAmountOneLineWithoutCost :: Bool -> MixedAmount -> String Source #

Get the one-line string representation of a mixed amount, but without any @ costs. With a True argument, adds ANSI codes to show negative amounts in red. See showMixedAmountB for special cases.

showMixedAmountElided :: Int -> Bool -> MixedAmount -> String Source #

Like showMixedAmountOneLineWithoutCost, but show at most the given width, with an elision indicator if there are more. With a True argument, adds ANSI codes to show negative amounts in red. See showMixedAmountB for special cases.

showMixedAmountWithZeroCommodity :: MixedAmount -> String Source #

Like showMixedAmount, but zero amounts are shown with their commodity if they have one. See showMixedAmountB for special cases.

showMixedAmountB :: AmountFormat -> MixedAmount -> WideBuilder Source #

Render a mixed amount using its amount display styles and the given amount format, as a builder for efficiency. (This can be converted to a Text with wbToText or to a String with wbUnpack).

Warning: this (and its showMixedAmount aliases above) basically assumes amounts have no costs. It can show misleading costs or not show costs which are there.

If a maximum width is given then:

  • If displayed on one line, it will display as many Amounts as can fit in the given width, and further Amounts will be elided. There will always be at least one amount displayed, even if this will exceed the requested maximum width.
  • If displayed on multiple lines, any Amounts longer than the maximum width will be elided.

Zero-equivalent amounts are shown as just "0".

The special "missing" amount is shown as the empty string (?).

showMixedAmountLinesB :: AmountFormat -> MixedAmount -> [WideBuilder] Source #

Helper for showMixedAmountB (and postingAsLines, ...) to show a list of Amounts on multiple lines. This returns the list of WideBuilders: one for each Amount, and padded/elided to the appropriate width. This does not honour displayOneLine; all amounts will be displayed as if displayOneLine were False.

wbToText :: WideBuilder -> Text Source #

Convert a WideBuilder to a strict Text.

wbUnpack :: WideBuilder -> String Source #

Convert a WideBuilder to a String.

mixedAmountSetPrecision :: AmountPrecision -> MixedAmount -> MixedAmount Source #

Set the display precision in the amount's commodities.

mixedAmountSetFullPrecision :: MixedAmount -> MixedAmount Source #

In each component amount, increase the display precision sufficiently to render it exactly (showing all significant decimal digits).

mixedAmountSetFullPrecisionUpTo :: Maybe Word8 -> MixedAmount -> MixedAmount Source #

In each component amount, increase the display precision sufficiently to render it exactly if possible, but not more than the given max precision, and if no max precision is given and the amount has infinite decimals, limit display precision to a hard-coded smaller number (8). See amountSetFullPrecisionUpTo.

mixedAmountSetPrecisionMin :: Word8 -> MixedAmount -> MixedAmount Source #

In each component amount, ensure the display precision is at least the given value. Makes all amounts have an explicit Precision.

mixedAmountSetPrecisionMax :: Word8 -> MixedAmount -> MixedAmount Source #

In each component amount, ensure the display precision is at most the given value. Makes all amounts have an explicit Precision.

misc.

Orphan instances