diff --git a/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.component.html b/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.component.html index fd881016a..51872c932 100644 --- a/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.component.html +++ b/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.component.html @@ -17,7 +17,7 @@ {{ diffChange.difficultyShorten }} - {{ diffChange.change >= 0 ? '+' : '' }}{{ formatNumber(diffChange.change, locale, '1.2-2') }}% + {{ diffChange.change >= 0 ? '+' : '' }}{{ diffChange.change | amountShortener }}% diff --git a/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.components.ts b/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.components.ts index 24c44fe05..5e8b3ded7 100644 --- a/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.components.ts +++ b/frontend/src/app/components/difficulty-adjustments-table/difficulty-adjustments-table.components.ts @@ -43,7 +43,7 @@ export class DifficultyAdjustmentsTable implements OnInit { const change = (data.difficulty[i].difficulty / data.difficulty[i - 1].difficulty - 1) * 100; tableData.push(Object.assign(data.difficulty[i], { - change: change, + change: Math.round(change * 100) / 100, difficultyShorten: formatNumber( data.difficulty[i].difficulty / selectedPowerOfTen.divider, this.locale, '1.2-2') + selectedPowerOfTen.unit diff --git a/frontend/src/app/services/api.service.ts b/frontend/src/app/services/api.service.ts index 142f26807..da228a833 100644 --- a/frontend/src/app/services/api.service.ts +++ b/frontend/src/app/services/api.service.ts @@ -153,7 +153,7 @@ export class ApiService { getBlocks$(from: number): Observable { return this.httpClient.get( - this.apiBasePath + this.apiBasePath + `/api/v1/blocks-extras` + + this.apiBaseUrl + this.apiBasePath + `/api/v1/blocks-extras` + (from !== undefined ? `/${from}` : ``) ); } diff --git a/frontend/src/app/shared/pipes/amount-shortener.pipe.ts b/frontend/src/app/shared/pipes/amount-shortener.pipe.ts index 49b452cd9..5c58b7513 100644 --- a/frontend/src/app/shared/pipes/amount-shortener.pipe.ts +++ b/frontend/src/app/shared/pipes/amount-shortener.pipe.ts @@ -1,22 +1,39 @@ import { Pipe, PipeTransform } from '@angular/core'; +// https://medium.com/@thunderroid/angular-short-number-suffix-pipe-1k-2m-3b-dded4af82fb4 + @Pipe({ name: 'amountShortener' }) export class AmountShortenerPipe implements PipeTransform { - transform(num: number, ...args: number[]): unknown { - const digits = args[0] || 1; - const lookup = [ - { value: 1, symbol: '' }, - { value: 1e3, symbol: 'k' }, - { value: 1e6, symbol: 'M' }, - { value: 1e9, symbol: 'G' }, - { value: 1e12, symbol: 'T' }, - { value: 1e15, symbol: 'P' }, - { value: 1e18, symbol: 'E' } + transform(number: number, args?: any): any { + if (isNaN(number)) return null; // will only work value is a number + if (number === null) return null; + if (number === 0) return null; + let abs = Math.abs(number); + const rounder = Math.pow(10, 1); + const isNegative = number < 0; // will also work for Negetive numbers + let key = ''; + + const powers = [ + { key: 'E', value: 10e18 }, + { key: 'P', value: 10e15 }, + { key: 'T', value: 10e12 }, + { key: 'B', value: 10e9 }, + { key: 'M', value: 10e6 }, + { key: 'K', value: 1000 } ]; - const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; - var item = lookup.slice().reverse().find((item) => num >= item.value); - return item ? (num / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : '0'; + + for (let i = 0; i < powers.length; i++) { + let reduced = abs / powers[i].value; + reduced = Math.round(reduced * rounder) / rounder; + if (reduced >= 1) { + abs = reduced; + key = powers[i].key; + break; + } + } + + return (isNegative ? '-' : '') + abs + key; } -} +} \ No newline at end of file