From bc1cd4e7e813e56c9ef54ef3f6fd0bf6afdc7d49 Mon Sep 17 00:00:00 2001 From: Robert Dyer Date: Mon, 1 Jul 2024 13:46:18 -0500 Subject: [PATCH] format percents better --- apply-interest.js | 10 ++++++---- utils.js | 5 +++++ zestimate.js | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/apply-interest.js b/apply-interest.js index 4b461c5..2f73955 100644 --- a/apply-interest.js +++ b/apply-interest.js @@ -1,5 +1,5 @@ const api = require('@actual-app/api'); -const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, getLastTransactionDate, openBudget } = require('./utils'); +const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, getLastTransactionDate, openBudget, showPercent } = require('./utils'); require("dotenv").config(); (async () => { @@ -17,7 +17,7 @@ require("dotenv").config(); if (note) { if (note.indexOf('interestRate:') > -1 && note.indexOf('interestDay:') > -1) { - const interestRate = parseFloat(note.split('interestRate:')[1].split(' ')[0]); + let interestRate = parseFloat(note.split('interestRate:')[1].split(' ')[0]); const interestDay = parseInt(note.split('interestDay:')[1].split(' ')[0]); const interestTransactionDate = new Date(); @@ -38,11 +38,13 @@ require("dotenv").config(); const balance = await getAccountBalance(account, interestTransactionDate); const compoundedInterest = Math.round(balance * (Math.pow(1 + interestRate / 12, 1) - 1)); + interestRate = showPercent(interestRate); + console.log(`== ${account.name} ==`); console.log(` -> Balance: ${balance}`); console.log(` as of ${lastDate}`); console.log(` -> # days: ${daysPassed}`); - console.log(` -> Interest: ${compoundedInterest}`) + console.log(` -> Interest: ${compoundedInterest} (${interestRate})`) if (compoundedInterest) { await api.importTransactions(account.id, [{ @@ -50,7 +52,7 @@ require("dotenv").config(); payee: payeeId, amount: compoundedInterest, cleared: true, - notes: `Interest for 1 month at ${100*interestRate}%`, + notes: `Interest for 1 month at ${interestRate}`, }]); } } diff --git a/utils.js b/utils.js index 2f2dc01..dd7def1 100644 --- a/utils.js +++ b/utils.js @@ -137,4 +137,9 @@ module.exports = { setTimeout(resolve, ms); }); }, + + showPercent: function (pct) { + return Number(pct).toLocaleString(undefined, + { style: 'percent', maximumFractionDigits: 2 }) + }, }; diff --git a/zestimate.js b/zestimate.js index ff6e838..4a3b11c 100644 --- a/zestimate.js +++ b/zestimate.js @@ -1,6 +1,6 @@ const api = require('@actual-app/api'); const jsdom = require("jsdom"); -const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, openBudget, sleep } = require('./utils'); +const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, openBudget, showPercent, sleep } = require('./utils'); require("dotenv").config(); async function getZestimate(URL) { @@ -56,7 +56,7 @@ async function getZestimate(URL) { amount: diff, cleared: true, reconciled: true, - notes: `Update Zestimate to ${zestimate * ownership / 100} (${zestimate / 100}*${ownership * 100}%)`, + notes: `Update Zestimate to ${zestimate * ownership / 100} (${zestimate / 100}*${showPercent(ownership)})`, }]); }