From e4a0bdff8d6f3b241f63013eaa9f17aedf680d0e Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 11 Feb 2020 09:47:26 -0800 Subject: [PATCH] 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. --- hledger-lib/Hledger/Data/Journal.hs | 19 ++++++++++++------- tests/journal/default-commodity.test | 17 +++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index e90f9f2d9..28c235702 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -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: diff --git a/tests/journal/default-commodity.test b/tests/journal/default-commodity.test index 0c7adfb46..db865cbd7 100644 --- a/tests/journal/default-commodity.test +++ b/tests/journal/default-commodity.test @@ -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