Skip to content
Permalink
Browse files

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.
  • Loading branch information
simonmichael committed Feb 11, 2020
1 parent 1741b60 commit e4a0bdff8d6f3b241f63013eaa9f17aedf680d0e
Showing with 19 additions and 17 deletions.
  1. +12 −7 hledger-lib/Hledger/Data/Journal.hs
  2. +7 −10 tests/journal/default-commodity.test
@@ -921,15 +921,20 @@ 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
inferredstyles = jinferredcommodities 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
-- the posting amounts in that commodity (excluding price amounts), ie:
@@ -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

0 comments on commit e4a0bdf

Please sign in to comment.
You can’t perform that action at this time.