hledger-lib-1.52.1: A library providing the core functionality of hledger

Safe Haskell | None
Language | GHC2021

Hledger.Data.Amount

Contents

- Commodity
- Amount
  - arithmetic
  - styles
  - rendering
- MixedAmount
  - arithmetic
  - styles
  - rendering
- misc.
- Orphan instances

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

- showCommoditySymbol :: Text -> Text
- isNonsimpleCommodityChar :: Char -> Bool
- quoteCommoditySymbolIfNeeded :: Text -> Text
- nullamt :: Amount
- missingamt :: Amount
- num :: Quantity -> Amount
- usd :: DecimalRaw Integer -> Amount
- eur :: DecimalRaw Integer -> Amount
- gbp :: DecimalRaw Integer -> Amount
- per :: Quantity -> Amount
- hrs :: Quantity -> Amount
- at :: Amount -> Amount -> Amount
- (@@) :: Amount -> Amount -> Amount
- amountWithCommodity :: CommoditySymbol -> Amount -> Amount
- amountCost :: Amount -> Amount
- amountIsZero :: Amount -> Bool
- amountLooksZero :: Amount -> Bool
- divideAmount :: Quantity -> Amount -> Amount
- multiplyAmount :: Quantity -> Amount -> Amount
- invertAmount :: Amount -> Amount
- amountstyle :: AmountStyle
- canonicaliseAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount
- styleAmount :: Map CommoditySymbol AmountStyle -> Amount -> Amount
- amountSetStyles :: Map CommoditySymbol AmountStyle -> Amount -> Amount
- amountStyleSetRounding :: Rounding -> AmountStyle -> AmountStyle
- amountStylesSetRounding :: Rounding -> Map CommoditySymbol AmountStyle -> Map CommoditySymbol AmountStyle
- amountUnstyled :: Amount -> Amount
- commodityStylesFromAmounts :: [Amount] -> Either String (Map CommoditySymbol AmountStyle)
- getAmounts :: Amount -> [Amount]
- data AmountFormat = AmountFormat {
  - displayCommodity :: Bool
  - displayZeroCommodity :: Bool
  - displayCommodityOrder :: Maybe [CommoditySymbol]
  - displayDigitGroups :: Bool
  - displayForceDecimalMark :: Bool
  - displayOneLine :: Bool
  - displayMinWidth :: Maybe Int
  - displayMaxWidth :: Maybe Int
  - displayCost :: Bool
  - displayCostBasis :: Bool
  - displayColour :: Bool
  - displayQuotes :: Bool
  }
- defaultFmt :: AmountFormat
- fullZeroFmt :: AmountFormat
- noCostFmt :: AmountFormat
- oneLineFmt :: AmountFormat
- oneLineNoCostFmt :: AmountFormat
- machineFmt :: AmountFormat
- showAmount :: Amount -> String
- showAmountWith :: AmountFormat -> Amount -> String
- showAmountB :: AmountFormat -> Amount -> WideBuilder
- showAmountCost :: Amount -> String
- showAmountCostB :: AmountFormat -> Amount -> WideBuilder
- showAmountCostBasis :: Amount -> String
- showAmountCostBasisB :: AmountFormat -> Amount -> WideBuilder
- cshowAmount :: Amount -> String
- showAmountWithZeroCommodity :: Amount -> String
- showAmountDebug :: Amount -> String
- showAmountWithoutCost :: Amount -> String
- amountSetPrecision :: AmountPrecision -> Amount -> Amount
- amountSetPrecisionMin :: Word8 -> Amount -> Amount
- amountSetPrecisionMax :: Word8 -> Amount -> Amount
- withPrecision :: Amount -> AmountPrecision -> Amount
- amountSetFullPrecision :: Amount -> Amount
- amountSetFullPrecisionUpTo :: Maybe Word8 -> Amount -> Amount
- amountInternalPrecision :: Amount -> Word8
- amountDisplayPrecision :: Amount -> Word8
- defaultMaxPrecision :: Word8
- setAmountInternalPrecision :: Word8 -> Amount -> Amount
- withInternalPrecision :: Amount -> Word8 -> Amount
- setAmountDecimalPoint :: Maybe Char -> Amount -> Amount
- withDecimalPoint :: Amount -> Maybe Char -> Amount
- amountStripCost :: Amount -> Amount
- nullmixedamt :: MixedAmount
- missingmixedamt :: MixedAmount
- isMissingMixedAmount :: MixedAmount -> Bool
- mixed :: Foldable t => t Amount -> MixedAmount
- mixedAmount :: Amount -> MixedAmount
- maAddAmount :: MixedAmount -> Amount -> MixedAmount
- maAddAmounts :: Foldable t => MixedAmount -> t Amount -> MixedAmount
- amounts :: MixedAmount -> [Amount]
- amountsRaw :: MixedAmount -> [Amount]
- amountsPreservingZeros :: MixedAmount -> [Amount]
- maCommodities :: MixedAmount -> Set CommoditySymbol
- filterMixedAmount :: (Amount -> Bool) -> MixedAmount -> MixedAmount
- filterMixedAmountByCommodity :: CommoditySymbol -> MixedAmount -> MixedAmount
- mapMixedAmount :: (Amount -> Amount) -> MixedAmount -> MixedAmount
- unifyMixedAmount :: MixedAmount -> Maybe Amount
- mixedAmountStripCosts :: MixedAmount -> MixedAmount
- mixedAmountCost :: MixedAmount -> MixedAmount
- maNegate :: MixedAmount -> MixedAmount
- maPlus :: MixedAmount -> MixedAmount -> MixedAmount
- maMinus :: MixedAmount -> MixedAmount -> MixedAmount
- maSum :: Foldable t => t MixedAmount -> MixedAmount
- divideMixedAmount :: Quantity -> MixedAmount -> MixedAmount
- multiplyMixedAmount :: Quantity -> MixedAmount -> MixedAmount
- averageMixedAmounts :: Foldable f => f MixedAmount -> MixedAmount
- sumAndAverageMixedAmounts :: Foldable f => f MixedAmount -> (MixedAmount, MixedAmount)
- isNegativeAmount :: Amount -> Bool
- isNegativeMixedAmount :: MixedAmount -> Maybe Bool
- mixedAmountIsZero :: MixedAmount -> Bool
- maIsZero :: MixedAmount -> Bool
- maIsNonZero :: MixedAmount -> Bool
- mixedAmountLooksZero :: MixedAmount -> Bool
- canonicaliseMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
- styleMixedAmount :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
- mixedAmountSetStyles :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
- mixedAmountUnstyled :: MixedAmount -> MixedAmount
- showMixedAmount :: MixedAmount -> String
- showMixedAmountWith :: AmountFormat -> MixedAmount -> String
- showMixedAmountOneLine :: MixedAmount -> String
- showMixedAmountDebug :: MixedAmount -> String
- showMixedAmountWithoutCost :: Bool -> MixedAmount -> String
- showMixedAmountOneLineWithoutCost :: Bool -> MixedAmount -> String
- showMixedAmountElided :: Int -> Bool -> MixedAmount -> String
- showMixedAmountWithZeroCommodity :: MixedAmount -> String
- showMixedAmountB :: AmountFormat -> MixedAmount -> WideBuilder
- showMixedAmountLinesB :: AmountFormat -> MixedAmount -> [WideBuilder]
- showMixedAmountLinesPartsB :: AmountFormat -> MixedAmount -> [(WideBuilder, Amount)]
- wbToText :: WideBuilder -> Text
- wbUnpack :: WideBuilder -> String
- mixedAmountSetPrecision :: AmountPrecision -> MixedAmount -> MixedAmount
- mixedAmountSetFullPrecision :: MixedAmount -> MixedAmount
- mixedAmountSetFullPrecisionUpTo :: Maybe Word8 -> MixedAmount -> MixedAmount
- mixedAmountSetPrecisionMin :: Word8 -> MixedAmount -> MixedAmount
- mixedAmountSetPrecisionMax :: Word8 -> MixedAmount -> MixedAmount
- tests_Amount :: TestTree

# Commodity

showCommoditySymbol :: Text -> Text Source #

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

isNonsimpleCommodityChar :: Char -> Bool Source #

quoteCommoditySymbolIfNeeded :: Text -> Text Source #

# 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.

num :: Quantity -> Amount Source #

usd :: DecimalRaw Integer -> Amount Source #

eur :: DecimalRaw Integer -> Amount Source #

gbp :: DecimalRaw Integer -> Amount Source #

per :: Quantity -> Amount Source #

hrs :: Quantity -> Amount Source #

at :: Amount -> Amount -> Amount Source #

(@@) :: Amount -> Amount -> Amount Source #

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). The amount's transacted price, if any, is not changed. An amount with zero quantity is left unchanged.

## 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.

commodityStylesFromAmounts :: [Amount] -> Either String (Map CommoditySymbol AmountStyle) Source #

Given a list of amounts, in parse order (roughly speaking; see journalStyleInfluencingAmounts), build a map from their commodity names to standard commodity display formats. Can return an error message eg if inconsistent number formats are found.

Though, these amounts may have come from multiple files, so we shouldn't assume they use consistent number formats. Currently we don't enforce that even within a single file, and this function never reports an error.

getAmounts :: Amount -> [Amount] Source #

Get an amount and its attached cost amount if any. Returns one or two amounts.

## 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

- displayCommodity :: Bool

  Whether to display commodity symbols.

- displayZeroCommodity :: Bool

  Whether to display commodity symbols for zero Amounts.

- displayCommodityOrder :: Maybe [CommoditySymbol]

  For a MixedAmount, an optional order in which to display the commodities. Also, causes 0s to be generated for any commodities which are not present (important for tabular reports).

- displayDigitGroups :: Bool

  Whether to display digit group marks (eg thousands separators)

- displayForceDecimalMark :: Bool

  Whether to add a trailing decimal mark when there are no decimal digits and there are digit group marks, to disambiguate

- displayOneLine :: Bool

  Whether to display on one line.

- displayMinWidth :: Maybe Int

  Minimum width to pad to

- displayMaxWidth :: Maybe Int

  Maximum width to clip to

- displayCost :: Bool

  Whether to display Amounts' costs.

- displayCostBasis :: Bool

  Whether to display Amounts' cost basis (Ledger-style lot syntax).

- displayColour :: Bool

  Whether to ansi-colourise negative Amounts.

- displayQuotes :: Bool

  Whether to enclose complex symbols in quotes (normally true)

#### Instances

Instances details

Default AmountFormat Source # | By default, display amounts using `defaultFmt` amount display options.
Instance details Defined in Hledger.Data.Amount Methods def :: AmountFormat Source #
Show AmountFormat Source # |
Instance details Defined in Hledger.Data.Amount Methods showsPrec :: Int -> AmountFormat -> ShowS Source # show :: AmountFormat -> String Source # showList :: [AmountFormat] -> ShowS Source #

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 or cost basis.

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.

showAmountCost :: Amount -> String Source #

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

showAmountCostBasis :: Amount -> String Source #

Show an amount's cost basis as Ledger-style lot syntax: {LOTCOST} LOTDATE.

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

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 `Amount`s.

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 :: Foldable f => f MixedAmount -> MixedAmount Source #

Calculate the average of some mixed amounts.

sumAndAverageMixedAmounts :: Foldable f => f MixedAmount -> (MixedAmount, MixedAmount) Source #

Calculate the sum and 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.

showMixedAmountLinesPartsB :: AmountFormat -> MixedAmount -> [(WideBuilder, Amount)] Source #

Like `showMixedAmountLinesB` but also returns the amounts associated with each text builder.

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.

tests_Amount :: TestTree Source #

# Orphan instances

Monoid MixedAmount Source # |
Instance details Methods mempty :: MixedAmount Source # mappend :: MixedAmount -> MixedAmount -> MixedAmount Source # mconcat :: [MixedAmount] -> MixedAmount Source #
Semigroup MixedAmount Source # |
Instance details Methods (<>) :: MixedAmount -> MixedAmount -> MixedAmount Source # sconcat :: NonEmpty MixedAmount -> MixedAmount Source # stimes :: Integral b => b -> MixedAmount -> MixedAmount Source #
Num Amount Source # |
Instance details Methods (+) :: Amount -> Amount -> Amount Source # (-) :: Amount -> Amount -> Amount Source # (*) :: Amount -> Amount -> Amount Source # negate :: Amount -> Amount Source # abs :: Amount -> Amount Source # signum :: Amount -> Amount Source # fromInteger :: Integer -> Amount Source #
Num MixedAmount Source # |
Instance details Methods (+) :: MixedAmount -> MixedAmount -> MixedAmount Source # (-) :: MixedAmount -> MixedAmount -> MixedAmount Source # (*) :: MixedAmount -> MixedAmount -> MixedAmount Source # negate :: MixedAmount -> MixedAmount Source # abs :: MixedAmount -> MixedAmount Source # signum :: MixedAmount -> MixedAmount Source # fromInteger :: Integer -> MixedAmount Source #
HasAmounts Amount Source # |
Instance details Methods styleAmounts :: Map CommoditySymbol AmountStyle -> Amount -> Amount Source #
HasAmounts BalanceData Source # |
Instance details Methods styleAmounts :: Map CommoditySymbol AmountStyle -> BalanceData -> BalanceData Source #
HasAmounts MixedAmount Source # |
Instance details Methods styleAmounts :: Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount Source #
HasAmounts a => HasAmounts (Account a) Source # |
Instance details Methods styleAmounts :: Map CommoditySymbol AmountStyle -> Account a -> Account a Source #
HasAmounts a => HasAmounts (PeriodData a) Source # |
Instance details Methods styleAmounts :: Map CommoditySymbol AmountStyle -> PeriodData a -> PeriodData a Source #