Skip to content

Commit 81cbebf

Browse files
committed
Use loop instead of if else condition for formatting large numbers
1 parent 4e964df commit 81cbebf

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

app.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,36 @@ def short_numeric_filter(value):
5454
"""
5555
Get the abbreviated numeric value.
5656
"""
57-
if value < 1000:
58-
return f"{value:.0f}"
59-
if value < 1000000:
60-
return f"{value / 1000:.1f}K"
61-
if value < 1000000000:
62-
return f"{value / 1000000:.1f}M"
63-
if value < 1000000000000:
64-
return f"{value / 1000000000:.1f}B"
65-
return f"{value / 1000000000000:.1f}T"
57+
units = [
58+
"",
59+
"K",
60+
"M",
61+
"B",
62+
"T",
63+
"Qa",
64+
"Qi",
65+
"Sx",
66+
"Sp",
67+
"O",
68+
"N",
69+
"D",
70+
"UD",
71+
"DD",
72+
"TD",
73+
"QaD",
74+
"QiD",
75+
"SxD",
76+
"SpD",
77+
"OD",
78+
"ND",
79+
"V",
80+
]
81+
i = 0
82+
mantissa = value
83+
while mantissa >= 1000:
84+
mantissa /= 1000
85+
i += 1
86+
return f"{mantissa:.3g}{units[i]}" if value >= 1000 else f"{value:.0f}"
6687

6788

6889
app.jinja_env.filters["short_numeric"] = short_numeric_filter

0 commit comments

Comments
 (0)