hledger-lib-1.20.4: A reusable library providing the core functionality of hledger

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 assigned Price, 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 price)
EUR 2 @@ $3 (total price)  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


When a mixed amount has been "normalised", it has no more than one amount in each commodity and no zero amounts; or it has just a single zero amount and no others.

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

Synopsis

# Amount

The empty simple amount.

The empty simple amount.

A temporary value for parsed transactions which had no amount specified.

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

## arithmetic

Convert a amount to its "cost" or "selling price" in another commodity, using its attached transaction price if it has one. Notes:

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

Is this amount exactly zero, ignoring its display precision ?

Does mixed amount appear to be zero when rendered with its display precision ?

Divide an amount's quantity by a constant.

Multiply an amount's quantity by a constant.

Divide an amount's quantity (and its total price, if it has one) by a constant. The total price will be kept positive regardless of the multiplier's sign.

Multiply an amount's quantity (and its total price, if it has one) by a constant. The total price will be kept positive regardless of the multiplier's sign.

Replace an amount's TotalPrice, if it has one, with an equivalent UnitPrice. Has no effect on amounts without one. Also increases the unit price's display precision to show one extra decimal place, to help keep transaction amounts balancing. Does Decimal division, might be some rounding/irrational number issues.

## rendering

Default amount style

Given a map of standard commodity display styles, apply the appropriate one to this amount. If there's no standard style for this amount's commodity, return the amount unchanged.

Like styleAmount, but keep the number of decimal places unchanged.

Reset this amount's display style to the default.

Get the string representation of an amount, based on its commodity's display settings. String representations equivalent to zero are converted to just "0". The special "missing" amount is displayed as the empty string.

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

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

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

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

Set an amount's display precision.

Set an amount's display precision, flipped.

Increase an amount's display precision, if needed, to enough decimal places to show it exactly (showing all significant decimal digits, excluding trailing zeros).

Set an amount's internal precision, ie rounds the Decimal representing the amount's quantity to some 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)". Does not change the amount's display precision. Intended only for internal use, eg when comparing amounts in tests.

Set an amount's internal precision, flipped. Intended only for internal use, eg when comparing amounts in tests.

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

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

Canonicalise an amount's display style using the provided commodity style map.

# MixedAmount

The empty mixed amount.

A temporary value for parsed transactions which had no amount specified.

mixed :: [Amount] -> MixedAmount Source #

Convert amounts in various commodities into a normalised MixedAmount.

Get a mixed amount's component amounts.

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

Return an unnormalised MixedAmount containing exactly one Amount with the specified commodity and the quantity of that commodity found in the original. NB if Amount's quantity is zero it will be discarded next time the MixedAmount gets normalised.

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

Like normaliseMixedAmount, but combine each commodity's amounts into just one by throwing away all prices except the first. This is only used as a rendering helper, and could show a misleading price.

Simplify a mixed amount's component amounts:

• amounts in the same commodity are combined unless they have different prices or total prices
• 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

Unify a MixedAmount to a single commodity value if possible. Like normaliseMixedAmount, 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.

## arithmetic

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

Divide a mixed amount's quantities by a constant.

Multiply a mixed amount's quantities by a constant.

Divide a mixed amount's quantities (and total prices, if any) by a constant. The total prices will be kept positive regardless of the multiplier's sign.

Multiply a mixed amount's quantities (and total prices, if any) by a constant. The total prices will be kept positive regardless of the multiplier's sign.

Calculate the average of some mixed amounts.

Is this amount negative ? The price is ignored.

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

Is this mixed amount exactly zero, ignoring display precisions ?

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

Replace each component amount's TotalPrice, if it has one, with an equivalent UnitPrice. Has no effect on amounts without one. Does Decimal division, might be some rounding/irrational number issues.

## rendering

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

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

Get the string representation of a mixed amount, after normalising it to one amount per commodity. Assumes amounts have no or similar prices, otherwise this can show misleading prices.

Get the one-line string representation of a mixed amount.

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

Get the string representation of a mixed amount, without showing any transaction prices. With a True argument, adds ANSI codes to show negative amounts in red.

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

Like showMixedAmountOneLineWithoutPrice, 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.

Like showMixedAmount, but zero amounts are shown with their commodity if they have one.

Get the string representation of a mixed amount, showing each of its component amounts with the specified precision, ignoring their commoditys' display precision settings.

showMixed :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int) Source #

General function to display a MixedAmount, one Amount on each line. It takes a function to display each Amount, an optional minimum width to pad to, an optional maximum width to display, and a Bool to determine whether to colourise negative numbers. Amounts longer than the maximum width (if given) will be elided. The function also returns the actual width of the output string.

showMixedUnnormalised :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int) Source #

Like showMixed, but does not normalise the MixedAmount before displaying.

showMixedOneLine :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int) Source #

General function to display a MixedAmount on a single line. It takes a function to display each Amount, an optional minimum width to pad to, an optional maximum width to display, and a Bool to determine whether to colourise negative numbers. It will display as many Amounts as it can in the maximum width (if given), and further Amounts will be elided. The function also returns the actual width of the output string.

showMixedOneLineUnnormalised :: (Amount -> String) -> Maybe Int -> Maybe Int -> Bool -> MixedAmount -> (String, Int) Source #

Like showMixedOneLine, but does not normalise the MixedAmount before displaying.

Set the display precision in the amount's commodities.

Canonicalise a mixed amount's display styles using the provided commodity style map.

# misc.

Compact labelled trace of a mixed amount, for debugging.

# Orphan instances

 Source # Instance details Methods Source # Instance details Methods(+) :: Amount -> Amount -> Amount #(-) :: Amount -> Amount -> Amount #(*) :: Amount -> Amount -> Amount #abs :: Amount -> Amount # Source # Instance details MethodsshowList :: [MarketPrice] -> ShowS #