Skip to content

Commit 5686c84

Browse files
Merge pull request #177 from jameel-institute/more-precision-for-percent-gdp
Make percent-of-GDP presentation more precise
2 parents f5e159f + 2b4a2ca commit 5686c84

6 files changed

Lines changed: 22 additions & 16 deletions

File tree

components/CostsTable.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ const displayValue = (scenario: Scenario, costId: string, metricId: string): str
175175
case CostBasis.PercentGDP:
176176
{
177177
const percentOfGdp = costAsPercentOfGdp(val, scenario.result.data?.gdp);
178-
if (percentOfGdp < 0.05 && percentOfGdp > 0) {
179-
return "<0.05%";
178+
if (percentOfGdp < 0.005 && percentOfGdp > 0) {
179+
return "<0.005%";
180180
}
181181
return humanReadablePercentOfGdp(percentOfGdp, signDisplay).percent;
182182
}

components/utils/formatters.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@ export const humanReadablePercentOfGdp = (
4040
num: number,
4141
signDisplay?: "exceptZero" | "auto" | "always",
4242
): { percent: string, reference: string } => {
43+
const reference = `of pre-pandemic GDP`;
44+
if (num < 0.005 && num > 0) {
45+
return { percent: "<0.005%", reference };
46+
}
47+
const numberOfSignificantDigits = Math.abs(num) < 1 ? 1 : 3;
4348
return {
4449
percent: Intl.NumberFormat(undefined, {
4550
style: "percent",
46-
minimumFractionDigits: num > 100 ? 0 : 1,
51+
minimumSignificantDigits: numberOfSignificantDigits,
52+
maximumSignificantDigits: numberOfSignificantDigits,
4753
signDisplay,
4854
}).format(num / 100),
49-
reference: `of pre-pandemic GDP`,
55+
reference,
5056
};
5157
};
5258

tests/e2e/helpers/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Object.freeze(parameterLabels);
1313

1414
export const costTolerance = 0.3;
1515

16-
export const decimalPercentMatcher = "(\\d{1,4}(\\.\\d)?%|<0\\.05%)";
16+
export const decimalPercentMatcher = "(\\d{1,4}(\\.(\\d*))?%|<0\\.005%)";
1717
export const decimalUSDMatcher = "(\\$\\d{1,3}(,\\d{3})*(\\.\\d{1,4})?[TBMK]|<\\$1 M)";
18-
export const decimalPercentMatcherAllowNegatives = "(-?\\d{1,4}(\\.\\d)?%|<0\\.05%)";
18+
export const decimalPercentMatcherAllowNegatives = "(-?\\d{1,4}(\\.(\\d*))?%|<0\\.005%)";
1919

2020
export const moneyTableRowLabels = [
2121
"(Net|Total) losses (relative to baseline )?(as % of GDP|\\(USD\\))",

tests/unit/components/Charts/utils/costCharts.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe("multi-scenario costs chart tooltip text for stacked column", () => {
174174
notDiffingHeader,
175175
false,
176176
{ color: "inherit", text: "74\\.0%.* of pre-pandemic GDP" },
177-
{ color: "inherit", text: "3\\.8%" },
177+
{ color: "inherit", text: "3\\.82%" },
178178
{ color: "inherit", text: "0\\.1%" },
179179
{ color: "inherit", text: "70\\.1%" },
180180
),
@@ -195,7 +195,7 @@ describe("multi-scenario costs chart tooltip text for stacked column", () => {
195195
diffingHeader,
196196
false,
197197
{ color: "darkgreen", text: "74\\.0%.* of pre-pandemic GDP", isNegative: true },
198-
{ color: "darkgreen", text: "3\\.8%", isNegative: true },
198+
{ color: "darkgreen", text: "3\\.82%", isNegative: true },
199199
{ color: "darkred", text: "0\\.1%" },
200200
{ color: "darkred", text: "70\\.1%" },
201201
),
@@ -262,7 +262,7 @@ describe("multi-scenario costs chart tooltip text for stacked column", () => {
262262
notDiffingHeader,
263263
false,
264264
{ color: "inherit", text: "74\\.0%.* of pre-pandemic GDP" },
265-
{ color: "inherit", text: "3\\.8%" },
265+
{ color: "inherit", text: "3\\.82%" },
266266
{ color: "inherit", text: "0\\.1%" },
267267
{ color: "inherit", text: "70\\.1%" },
268268
),
@@ -282,8 +282,8 @@ describe("multi-scenario costs chart tooltip text for stacked column", () => {
282282
hospitalParamOption,
283283
diffingHeader,
284284
false,
285-
{ color: "darkgreen", text: "7,301\\.0%.* of pre-pandemic GDP", isNegative: true },
286-
{ color: "darkgreen", text: "3\\.8%", isNegative: true },
285+
{ color: "darkgreen", text: "7,300%.* of pre-pandemic GDP", isNegative: true },
286+
{ color: "darkgreen", text: "3\\.82%", isNegative: true },
287287
{ color: "darkred", text: "0\\.1%" },
288288
{ color: "darkred", text: "7,000%" },
289289
),

tests/unit/components/CostsTable.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ const openVslModal = async (component: VueWrapper) => {
5454

5555
const checkPercentText = (costUSD: number, gdp: number, componentText: string) => {
5656
const percent = costAsPercentOfGdp(costUSD, gdp);
57-
if (percent < 0.05) {
58-
expect(componentText).toContain("<0.05%");
59-
} else {
60-
expect(componentText).toContain(percent.toFixed(1));
57+
if (percent < 0.005) {
58+
expect(componentText).toContain("<0.005%");
59+
} else if (percent < 1) {
60+
expect(componentText).toContain(percent.toFixed(0));
6161
}
6262
};
6363

tests/unit/components/utils/formatters.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ describe("humanReadablePercentOfGdp", () => {
3030
it("should format number as percent of GDP", () => {
3131
expect(humanReadablePercentOfGdp(10)).toEqual({ percent: "10.0%", reference: "of pre-pandemic GDP" });
3232
expect(humanReadablePercentOfGdp(200.5)).toEqual({ percent: "201%", reference: "of pre-pandemic GDP" });
33-
expect(humanReadablePercentOfGdp(12345.678)).toEqual({ percent: "12,346%", reference: "of pre-pandemic GDP" });
33+
expect(humanReadablePercentOfGdp(12345.678)).toEqual({ percent: "12,300%", reference: "of pre-pandemic GDP" });
3434
});
3535
});

0 commit comments

Comments
 (0)