Compare commits

...

1 Commits

Author SHA1 Message Date
natsoni
865015a193
Adaptive decimal number length in amount shortener 2024-12-07 21:31:49 +01:00
2 changed files with 13 additions and 6 deletions

View File

@ -310,21 +310,21 @@ export class AddressGraphComponent implements OnChanges, OnDestroy {
formatter: (val): string => { formatter: (val): string => {
let valSpan = maxValue - (this.period === 'all' ? 0 : minValue); let valSpan = maxValue - (this.period === 'all' ? 0 : minValue);
if (valSpan > 100_000_000_000) { if (valSpan > 100_000_000_000) {
return `${this.amountShortenerPipe.transform(Math.round(val / 100_000_000), 0, undefined, true)} BTC`; return `${this.amountShortenerPipe.transform(Math.round(val / 100_000_000), 0, undefined, true, true)} BTC`;
} }
else if (valSpan > 1_000_000_000) { else if (valSpan > 1_000_000_000) {
return `${this.amountShortenerPipe.transform(Math.round(val / 100_000_000), 2, undefined, true)} BTC`; return `${this.amountShortenerPipe.transform(Math.round(val / 100_000_000), 2, undefined, true, true)} BTC`;
} else if (valSpan > 100_000_000) { } else if (valSpan > 100_000_000) {
return `${(val / 100_000_000).toFixed(1)} BTC`; return `${(val / 100_000_000).toFixed(1)} BTC`;
} else if (valSpan > 10_000_000) { } else if (valSpan > 10_000_000) {
return `${(val / 100_000_000).toFixed(2)} BTC`; return `${(val / 100_000_000).toFixed(2)} BTC`;
} else if (valSpan > 1_000_000) { } else if (valSpan > 1_000_000) {
if (maxValue > 100_000_000_000) { if (maxValue > 100_000_000_000) {
return `${this.amountShortenerPipe.transform(Math.round(val / 100_000_000), 3, undefined, true)} BTC`; return `${this.amountShortenerPipe.transform(Math.round(val / 100_000_000), 3, undefined, true, true)} BTC`;
} }
return `${(val / 100_000_000).toFixed(3)} BTC`; return `${(val / 100_000_000).toFixed(3)} BTC`;
} else { } else {
return `${this.amountShortenerPipe.transform(val, 0, undefined, true)} sats`; return `${this.amountShortenerPipe.transform(val, 0, undefined, true, true)} sats`;
} }
} }
}, },
@ -338,7 +338,7 @@ export class AddressGraphComponent implements OnChanges, OnDestroy {
axisLabel: { axisLabel: {
color: 'rgb(110, 112, 121)', color: 'rgb(110, 112, 121)',
formatter: function(val) { formatter: function(val) {
return `$${this.amountShortenerPipe.transform(val, 0, undefined, true)}`; return `$${this.amountShortenerPipe.transform(val, 0, undefined, true, true)}`;
}.bind(this) }.bind(this)
}, },
splitLine: { splitLine: {

View File

@ -5,9 +5,10 @@ import { Pipe, PipeTransform } from '@angular/core';
}) })
export class AmountShortenerPipe implements PipeTransform { export class AmountShortenerPipe implements PipeTransform {
transform(num: number, ...args: any[]): unknown { transform(num: number, ...args: any[]): unknown {
const digits = args[0] ?? 1; let digits = args[0] ?? 1;
const unit = args[1] || undefined; const unit = args[1] || undefined;
const isMoney = args[2] || false; const isMoney = args[2] || false;
const addMoreDigits = args[3] || false;
if (num < 1000) { if (num < 1000) {
return num.toFixed(digits); return num.toFixed(digits);
@ -25,6 +26,12 @@ export class AmountShortenerPipe implements PipeTransform {
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
const item = lookup.slice().reverse().find((item) => num >= item.value); const item = lookup.slice().reverse().find((item) => num >= item.value);
if (addMoreDigits) {
// Add more decimal digits if the integer part is small
const integerPartLength = Math.floor(num / item.value).toString().length;
digits += Math.max(0, 3 - integerPartLength);
}
if (unit !== undefined) { if (unit !== undefined) {
return item ? (num / item.value).toFixed(digits).replace(rx, '$1') + ' ' + item.symbol + unit : '0'; return item ? (num / item.value).toFixed(digits).replace(rx, '$1') + ' ' + item.symbol + unit : '0';
} else { } else {