a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
48 lines
1014 B
Go
48 lines
1014 B
Go
package ledger
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// FormatMoney renders an amount as a decimal string with its currency.
|
|
func FormatMoney(m Money) string {
|
|
sign := ""
|
|
minor := m.Minor
|
|
if minor < 0 {
|
|
sign = "-"
|
|
minor = -minor
|
|
}
|
|
major := minor / 100
|
|
cents := minor % 100
|
|
return fmt.Sprintf("%s%d.%02d %s", sign, major, cents, m.Currency)
|
|
}
|
|
|
|
// FormatTransaction renders a transaction as a multi-line string.
|
|
func FormatTransaction(tx Transaction) string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "tx %s: %s\n", tx.ID, tx.Memo)
|
|
for _, e := range tx.Entries {
|
|
fmt.Fprintf(&b, " %s %s %s\n", e.AccountID, FormatMoney(e.Amount), e.Memo)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// FormatKind returns a human label for an account kind.
|
|
func FormatKind(k AccountKind) string {
|
|
switch k {
|
|
case AccountAsset:
|
|
return "asset"
|
|
case AccountLiability:
|
|
return "liability"
|
|
case AccountEquity:
|
|
return "equity"
|
|
case AccountRevenue:
|
|
return "revenue"
|
|
case AccountExpense:
|
|
return "expense"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|