mirror of
https://github.com/mempool/mempool.git
synced 2026-08-13 12:33:11 +02:00
Merge 76cd9c2482 into 29afce710d
This commit is contained in:
commit
13904233c4
6 changed files with 33 additions and 14 deletions
|
|
@ -230,7 +230,7 @@ export class BlockFeesGraphComponent implements OnInit {
|
|||
axisLabel: {
|
||||
color: 'rgb(110, 112, 121)',
|
||||
formatter: function(val) {
|
||||
return this.fiatShortenerPipe.transform(val, null, this.currency);
|
||||
return this.fiatShortenerPipe.transform(val, this.currency);
|
||||
}.bind(this)
|
||||
},
|
||||
splitLine: {
|
||||
|
|
|
|||
|
|
@ -329,7 +329,7 @@ export class BlockFeesSubsidyGraphComponent implements OnInit {
|
|||
axisLabel: {
|
||||
color: 'var(--grey)',
|
||||
formatter: function(val) {
|
||||
return this.fiatShortenerPipe.transform(val, null, 'USD');
|
||||
return this.fiatShortenerPipe.transform(val, 'USD');
|
||||
}.bind(this)
|
||||
},
|
||||
splitLine: {
|
||||
|
|
|
|||
|
|
@ -238,7 +238,7 @@ export class BlockRewardsGraphComponent implements OnInit {
|
|||
axisLabel: {
|
||||
color: 'rgb(110, 112, 121)',
|
||||
formatter: function(val) {
|
||||
return this.fiatShortenerPipe.transform(val, null, this.currency);
|
||||
return this.fiatShortenerPipe.transform(val, this.currency);
|
||||
}.bind(this)
|
||||
},
|
||||
splitLine: {
|
||||
|
|
|
|||
|
|
@ -213,7 +213,7 @@ export class PriceChartComponent implements OnInit {
|
|||
axisLabel: {
|
||||
color: 'rgb(110, 112, 121)',
|
||||
formatter: function(val) {
|
||||
return this.fiatShortenerPipe.transform(val, null, this.currency);
|
||||
return this.fiatShortenerPipe.transform(val, this.currency);
|
||||
}.bind(this)
|
||||
},
|
||||
splitLine: {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@ import { Inject, LOCALE_ID, OnDestroy, Pipe, PipeTransform } from '@angular/core
|
|||
import { Subscription } from 'rxjs';
|
||||
import { StateService } from '@app/services/state.service';
|
||||
|
||||
// Below this threshold, show 2 decimal places even when digitsInfo requests 0,
|
||||
// so small fiat amounts (e.g. $3.50) remain readable instead of rounding to $4.
|
||||
// Below this threshold, fiatCurrency shows 2 decimal places even when digitsInfo
|
||||
// requests 0 (so e.g. $3.50 stays readable instead of rounding to $4), and
|
||||
// fiatShortener displays the full formatted value instead of abbreviating with k/M/B suffixes.
|
||||
export const SMALL_FIAT_THRESHOLD = 1000;
|
||||
|
||||
@Pipe({
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import { SMALL_FIAT_THRESHOLD } from './fiat-currency.pipe';
|
|||
export class FiatShortenerPipe implements PipeTransform, OnDestroy {
|
||||
fiatSubscription: Subscription;
|
||||
currency: string;
|
||||
private currencyMaxFracCache: Record<string, number> = {};
|
||||
|
||||
constructor(
|
||||
@Inject(LOCALE_ID) public locale: string,
|
||||
|
|
@ -24,12 +25,26 @@ export class FiatShortenerPipe implements PipeTransform, OnDestroy {
|
|||
this.fiatSubscription.unsubscribe();
|
||||
}
|
||||
|
||||
transform(num: number, ...args: any[]): unknown {
|
||||
const digits = args[0] || 1;
|
||||
const currency = args[1] || this.currency || 'USD';
|
||||
private getCurrencyMaxFrac(currency: string): number {
|
||||
if (!(currency in this.currencyMaxFracCache)) {
|
||||
this.currencyMaxFracCache[currency] =
|
||||
new Intl.NumberFormat(this.locale, { style: 'currency', currency }).resolvedOptions().maximumFractionDigits ??
|
||||
Number.POSITIVE_INFINITY;
|
||||
}
|
||||
return this.currencyMaxFracCache[currency];
|
||||
}
|
||||
|
||||
transform(num: number, currencyInput?: string): unknown {
|
||||
let currency = currencyInput || this.currency || 'USD';
|
||||
try {
|
||||
this.getCurrencyMaxFrac(currency); // validates & warms cache
|
||||
} catch {
|
||||
currency = 'USD';
|
||||
}
|
||||
|
||||
if (Math.abs(num) < SMALL_FIAT_THRESHOLD) {
|
||||
return new Intl.NumberFormat(this.locale, { style: 'currency', currency, maximumFractionDigits: 1 }).format(num);
|
||||
const maxFrac = Math.min(2, this.getCurrencyMaxFrac(currency));
|
||||
return new Intl.NumberFormat(this.locale, { style: 'currency', currency, minimumFractionDigits: maxFrac, maximumFractionDigits: maxFrac }).format(num);
|
||||
}
|
||||
|
||||
const lookup = [
|
||||
|
|
@ -41,11 +56,14 @@ export class FiatShortenerPipe implements PipeTransform, OnDestroy {
|
|||
{ value: 1e15, symbol: 'P' },
|
||||
{ value: 1e18, symbol: 'E' }
|
||||
];
|
||||
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
|
||||
const item = lookup.slice().reverse().find((item) => num >= item.value);
|
||||
const absNum = Math.abs(num);
|
||||
const item = lookup.slice().reverse().find((item) => absNum >= item.value);
|
||||
|
||||
let result = item ? (num / item.value).toFixed(digits).replace(rx, '$1') : '0';
|
||||
result = new Intl.NumberFormat(this.locale, { style: 'currency', currency, maximumFractionDigits: 0 }).format(item ? num / item.value : 0);
|
||||
if (!item) {
|
||||
return new Intl.NumberFormat(this.locale, { style: 'currency', currency, maximumFractionDigits: 0 }).format(0);
|
||||
}
|
||||
|
||||
const result = new Intl.NumberFormat(this.locale, { style: 'currency', currency, maximumFractionDigits: 0 }).format(num / item.value);
|
||||
|
||||
return result + item.symbol;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue