Extract amount formatting to Format.
This commit is contained in:
parent
eca33a3bf6
commit
8060c92092
5 changed files with 11 additions and 10 deletions
|
@ -1,5 +1,4 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from decimal import Decimal, getcontext
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
|
@ -9,3 +8,9 @@ class Format:
|
||||||
thousands: str = ""
|
thousands: str = ""
|
||||||
symbol: str = "rightspace"
|
symbol: str = "rightspace"
|
||||||
datesep: str = "-"
|
datesep: str = "-"
|
||||||
|
|
||||||
|
def format_num(self, num):
|
||||||
|
parts = f"{num:,}".split(".")
|
||||||
|
parts[0] = parts[0].replace(",", self.thousands)
|
||||||
|
result = self.decimal.join(parts)
|
||||||
|
return result
|
||||||
|
|
|
@ -8,9 +8,8 @@ class Beancount(BaseOutput):
|
||||||
lines = []
|
lines = []
|
||||||
for price in series.prices:
|
for price in series.prices:
|
||||||
|
|
||||||
amount_parts = f"{price.amount:,}".split(".")
|
amount = fmt.format_num(price.amount)
|
||||||
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
|
# TODO warn if fmt settings make an invalid number
|
||||||
amount = ".".join(amount_parts)
|
|
||||||
|
|
||||||
qa_parts = [amount]
|
qa_parts = [amount]
|
||||||
if fmt.symbol == "right":
|
if fmt.symbol == "right":
|
||||||
|
|
|
@ -8,9 +8,7 @@ class CSV(BaseOutput):
|
||||||
lines = ["date,base,quote,amount,source,type"]
|
lines = ["date,base,quote,amount,source,type"]
|
||||||
for price in series.prices:
|
for price in series.prices:
|
||||||
date = str(price.date).replace("-", fmt.datesep)
|
date = str(price.date).replace("-", fmt.datesep)
|
||||||
amount_parts = f"{price.amount:,}".split(".")
|
amount = fmt.format_num(price.amount)
|
||||||
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
|
|
||||||
amount = fmt.decimal.join(amount_parts)
|
|
||||||
line = ",".join(
|
line = ",".join(
|
||||||
[date, series.base, series.quote, amount, source.id(), series.type]
|
[date, series.base, series.quote, amount, source.id(), series.type]
|
||||||
)
|
)
|
||||||
|
|
|
@ -29,6 +29,7 @@ class GnuCashSQL(BaseOutput):
|
||||||
).encode("utf-8")
|
).encode("utf-8")
|
||||||
)
|
)
|
||||||
guid = m.hexdigest()[0:32]
|
guid = m.hexdigest()[0:32]
|
||||||
|
# TODO extract this logic to a helper method
|
||||||
value_num = str(price.amount).replace(".", "")
|
value_num = str(price.amount).replace(".", "")
|
||||||
value_denom = 10 ** len(f"{price.amount}.".split(".")[1])
|
value_denom = 10 ** len(f"{price.amount}.".split(".")[1])
|
||||||
v = (
|
v = (
|
||||||
|
|
|
@ -9,9 +9,7 @@ class Ledger(BaseOutput):
|
||||||
for price in series.prices:
|
for price in series.prices:
|
||||||
date = str(price.date).replace("-", fmt.datesep)
|
date = str(price.date).replace("-", fmt.datesep)
|
||||||
|
|
||||||
amount_parts = f"{price.amount:,}".split(".")
|
amount = fmt.format_num(price.amount)
|
||||||
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
|
|
||||||
amount = fmt.decimal.join(amount_parts)
|
|
||||||
|
|
||||||
qa_parts = [amount]
|
qa_parts = [amount]
|
||||||
if fmt.symbol == "left":
|
if fmt.symbol == "left":
|
||||||
|
|
Loading…
Add table
Reference in a new issue