DrCr/src/display.ts
2024-11-16 01:20:18 +11:00

42 lines
1.4 KiB
TypeScript

/*
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { db } from './db.ts';
export function pp(quantity: number): string {
// Pretty print the quantity
if (quantity < 0) {
return '−' + pp(-quantity);
}
const factor = Math.pow(10, db.metadata.dps);
const wholePart = Math.floor(quantity / factor);
const fracPart = quantity % factor;
return wholePart.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '\u202F') + '.' + fracPart.toString().padStart(db.metadata.dps, '0');
}
export function ppWithCommodity(quantity: number, commodity: string): string {
// Pretty print the amount including commodity
if (commodity.length === 1) {
return commodity + pp(quantity);
} else {
return pp(quantity) + ' ' + commodity;
}
}