Skip to content

Commit 9730ff3

Browse files
committed
cli: format currencies in "Indian" format
1 parent 19528ac commit 9730ff3

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

casparser/cli.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
console = Console()
2626

2727

28+
def formatINR(number):
29+
"""format a number as INR
30+
credit: https://stackoverflow.com/a/68484491"""
31+
prefix = {True: "-", False: ""}
32+
number = float(number)
33+
number = round(number, 2)
34+
is_negative = number < 0
35+
number = abs(number)
36+
s, *d = str(number).partition(".")
37+
r = ",".join([s[x - 2 : x] for x in range(-3, -len(s), -2)][::-1] + [s[-3:]])
38+
value = "".join([r] + d)
39+
return f"{prefix[is_negative]}{value}"
40+
41+
2842
def validate_fy(ctx, param, value):
2943
return re.search(r"FY\d{4}-\d{2,4}", value, re.I) is not None
3044

@@ -128,7 +142,7 @@ def print_summary(parsed_data: CASData, output_filename=None, include_zero_folio
128142
"scheme": scheme_name,
129143
"open": scheme["open"],
130144
"close": scheme["close"] if is_summary else f"{scheme['close']}\n/\n{calc_close}",
131-
"value": f"{valuation['value']:,.2f}\n@\n{valuation['nav']:,.2f}",
145+
"value": f"{formatINR(valuation['value'])}\n@\n{formatINR(valuation['nav'])}",
132146
"txns": len(scheme["transactions"]),
133147
"status": status,
134148
}
@@ -143,12 +157,12 @@ def print_summary(parsed_data: CASData, output_filename=None, include_zero_folio
143157
table.add_row(*[str(row[key]) for key in console_header.keys()])
144158
console.print(table)
145159
if cost > 0:
146-
console.print(f"Portfolio Cost Value : [bold green]{cost:,.2f}[/]")
160+
console.print(f"Portfolio Cost Value : [bold green]{formatINR(cost)}[/]")
147161
gains = value - cost
148162
color = "red" if gains < 0 else "green"
149-
console.print(f"Portfolio Gains : [bold {color}]{gains:,.2f}[/]")
163+
console.print(f"Portfolio Gains : [bold {color}]{formatINR(gains)}[/]")
150164
console.print(
151-
f"Portfolio Valuation : [bold green]{value:,.2f}[/] "
165+
f"Portfolio Valuation : [bold green]{formatINR(value)}[/] "
152166
f"[As of {data['statement_period']['to']}]"
153167
)
154168
console.print("[bold]Summary[/]")
@@ -191,16 +205,16 @@ def print_gains(parsed_data: CASData, output_file_path=None, gains_112a=""):
191205
table.add_row(
192206
"",
193207
fund,
194-
f"{round(ltcg, 2)}",
195-
f"{round(ltcg_taxable, 2)}",
196-
f"{round(stcg, 2)}",
208+
f"{formatINR(ltcg)}",
209+
f"{formatINR(ltcg_taxable)}",
210+
f"{formatINR(stcg)}",
197211
)
198212
table.add_row(
199213
"",
200214
f"[bold]{fy} - Total Gains[/]",
201-
f"[bold {get_color(ltcg_total)}]{round(ltcg_total, 2)}[/]",
202-
f"[bold {get_color(ltcg_taxable_total)}]{round(ltcg_taxable_total, 2)}[/]",
203-
f"[bold {get_color(stcg_total)}]{round(stcg_total, 2)}[/]",
215+
f"[bold {get_color(ltcg_total)}]{formatINR(ltcg_total)}[/]",
216+
f"[bold {get_color(ltcg_taxable_total)}]{formatINR(ltcg_taxable_total)}[/]",
217+
f"[bold {get_color(stcg_total)}]{formatINR(stcg_total)}[/]",
204218
)
205219
console.print(table)
206220

@@ -235,10 +249,10 @@ def print_gains(parsed_data: CASData, output_file_path=None, gains_112a=""):
235249
console.print(Markdown("\n".join(md_txt)))
236250

237251
console.print(f"\n[bold]PnL[/] as of [bold]{data['statement_period']['to']}[/]")
238-
console.print(f"{'Total Invested':20s}: [bold]{cg.invested_amount:,.2f}[/]")
239-
console.print(f"{'Current Valuation':20s}: [bold]{cg.current_value:,.2f}[/]")
252+
console.print(f"{'Total Invested':20s}: [bold]{formatINR(cg.invested_amount)}[/]")
253+
console.print(f"{'Current Valuation':20s}: [bold]{formatINR(cg.current_value)}[/]")
240254
pnl = cg.current_value - cg.invested_amount
241-
console.print(f"{'Absolute PnL':20s}: [bold {get_color(pnl)}]{pnl:,.2f}[/]")
255+
console.print(f"{'Absolute PnL':20s}: [bold {get_color(pnl)}]{formatINR(pnl)}[/]")
242256
console.print(
243257
"\n[bold yellow]Warning:[/] Capital gains module is in beta stage. "
244258
"Please verify the generated data manually."

pyproject.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@ target-version = ['py38']
5353

5454
[tool.pytest.ini_options]
5555
minversion = "7.0"
56-
addopts = "--cov=casparser --cov-report=xml --cov-report=html --cov-config=tox.ini"
56+
addopts = "--cov=casparser --cov-report=xml --cov-report=html"
5757
testpaths = [
5858
"tests",
5959
]
6060

6161
[tool.ruff]
6262
line-length = 100
6363
target-version = "py38"
64+
65+
[tool.coverage.report]
66+
omit = [
67+
"casparser/cli.py"
68+
]
69+
precision = 3
70+
71+
[tool.coverage.run]
72+
omit = [
73+
"casparser/cli.py"
74+
]

tox.ini

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)