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 decimal import Decimal, getcontext
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
@ -9,3 +8,9 @@ class Format:
|
|||
thousands: str = ""
|
||||
symbol: str = "rightspace"
|
||||
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 = []
|
||||
for price in series.prices:
|
||||
|
||||
amount_parts = f"{price.amount:,}".split(".")
|
||||
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
|
||||
amount = ".".join(amount_parts)
|
||||
amount = fmt.format_num(price.amount)
|
||||
# TODO warn if fmt settings make an invalid number
|
||||
|
||||
qa_parts = [amount]
|
||||
if fmt.symbol == "right":
|
||||
|
|
|
@ -8,9 +8,7 @@ class CSV(BaseOutput):
|
|||
lines = ["date,base,quote,amount,source,type"]
|
||||
for price in series.prices:
|
||||
date = str(price.date).replace("-", fmt.datesep)
|
||||
amount_parts = f"{price.amount:,}".split(".")
|
||||
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
|
||||
amount = fmt.decimal.join(amount_parts)
|
||||
amount = fmt.format_num(price.amount)
|
||||
line = ",".join(
|
||||
[date, series.base, series.quote, amount, source.id(), series.type]
|
||||
)
|
||||
|
|
|
@ -29,6 +29,7 @@ class GnuCashSQL(BaseOutput):
|
|||
).encode("utf-8")
|
||||
)
|
||||
guid = m.hexdigest()[0:32]
|
||||
# TODO extract this logic to a helper method
|
||||
value_num = str(price.amount).replace(".", "")
|
||||
value_denom = 10 ** len(f"{price.amount}.".split(".")[1])
|
||||
v = (
|
||||
|
|
|
@ -9,9 +9,7 @@ class Ledger(BaseOutput):
|
|||
for price in series.prices:
|
||||
date = str(price.date).replace("-", fmt.datesep)
|
||||
|
||||
amount_parts = f"{price.amount:,}".split(".")
|
||||
amount_parts[0] = amount_parts[0].replace(",", fmt.thousands)
|
||||
amount = fmt.decimal.join(amount_parts)
|
||||
amount = fmt.format_num(price.amount)
|
||||
|
||||
qa_parts = [amount]
|
||||
if fmt.symbol == "left":
|
||||
|
|
Loading…
Add table
Reference in a new issue