journal: default commodity (D) limits display precision too (#1187)

D directives are now fully equivalent to commodity directives for
setting a commodity's display style. (Previously it was equivalent to
a posting amount, so it couldn't limit the number of decimal places.)
When both kinds of directive exist, commodity directives take precedence.
When there are multiple D directives in the journal, only the last one
affects display style.
This commit is contained in:
Simon Michael 2020-02-11 09:47:26 -08:00
parent 1741b607e2
commit e4a0bdff8d
2 changed files with 19 additions and 17 deletions

View File

@ -921,14 +921,19 @@ journalApplyCommodityStyles j@Journal{jtxns=ts, jpricedirectives=pds} =
fixbalanceassertion ba = ba{baamount=styleAmount styles $ baamount ba}
fixpricedirective pd@PriceDirective{pdamount=a} = pd{pdamount=styleAmountExceptPrecision styles a}
-- | Get all the amount styles defined in this journal, either declared by
-- a commodity directive or inferred from amounts, as a map from symbol to style.
-- Styles declared by commodity directives take precedence, and these also are
-- guaranteed to know their decimal point character.
-- | Get the canonical amount styles for this journal, whether
-- declared by commodity directives, by the last default commodity (D)
-- directive, or inferred from posting amounts, as a map from symbol
-- to style. Styles declared by directives take precedence (and
-- commodity takes precedence over D). Styles from directives are
-- guaranteed to specify the decimal mark character.
journalCommodityStyles :: Journal -> M.Map CommoditySymbol AmountStyle
journalCommodityStyles j = declaredstyles <> inferredstyles
journalCommodityStyles j =
-- XXX could be some redundancy here, cf journalStyleInfluencingAmounts
commoditystyles <> defaultcommoditystyle <> inferredstyles
where
declaredstyles = M.mapMaybe cformat $ jcommodities j
commoditystyles = M.mapMaybe cformat $ jcommodities j
defaultcommoditystyle = M.fromList $ catMaybes [jparsedefaultcommodity j]
inferredstyles = jinferredcommodities j
-- | Collect and save inferred amount styles for each commodity based on

View File

@ -56,19 +56,16 @@ $ hledger -f- print
>=0
# 5. commodity and display style applied to the second posting amount..
# which ends up setting the digit group style, since it's the first amount
# with digit groups. The great precision is used.
# 5. A D directive affects all aspects of display style,
# including limiting the display precision, like a commodity directive (#1187).
<
D $1,000.0
2010/1/1
(a) $1000000.00
(b) 1000000
D 1,000.0 A
1/1
(a) 1000.123
$ hledger -f- print
2010-01-01
(a) $1,000,000.00
(b) $1,000,000.00
2020-01-01
(a) 1,000.1 A
>=0