Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved number formatting #481

Merged
merged 12 commits into from
Nov 3, 2022
4 changes: 4 additions & 0 deletions core/src/helpers/formatToAutomaticDecimalPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ function limitedValue(value: number | BigNumber): number | BigNumber {
return value;
}

export function formatWithThousandSeparators(value: string): string {
return value.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ',');
zoey-kaiser marked this conversation as resolved.
Show resolved Hide resolved
}

export function formatToAutomaticDecimalPoints(
value: number | BigNumber,
decimalPlaces: number = DECIMAL_PLACES_DEFAULT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ storiesOf('Common/Formatters/AnimatedNumber', module)
...common,
template: `<AnimatedNumber :value="num"/>`,
}))
.add('Formatted', () => ({
...common,
template: `<AnimatedNumber :value="2220309349.12" :formatted="true" />`,
}))
zoey-kaiser marked this conversation as resolved.
Show resolved Hide resolved
.add('Long Duration', () => ({
...common,
template: `<AnimatedNumber :value="num" :duration="5000"/>`,
Expand Down
16 changes: 15 additions & 1 deletion frontend/components/common/formatters/AnimatedNumber.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<span v-if="isValidNumber"
><span v-if="isValueSmallButNotZero">under </span
><animated-number :value="format(value)" :format-value="format" :duration="duration"
><animated-number :value="safeValue" :format-value="format" :duration="duration"
/></span>
</template>

Expand All @@ -13,6 +13,7 @@ import {
isValidNumber,
isValueSmallButNotZero,
formatToAutomaticDecimalPoints,
formatWithThousandSeparators,
} from 'auctions-core/src/helpers/formatToAutomaticDecimalPoints';

export default Vue.extend({
Expand All @@ -32,8 +33,18 @@ export default Vue.extend({
type: Number,
default: undefined,
},
formatted: {
type: Boolean,
default: false,
},
zoey-kaiser marked this conversation as resolved.
Show resolved Hide resolved
},
computed: {
safeValue(): number {
if (BigNumber.isBigNumber(this.value)) {
return this.value.toNumber();
}
return this.value;
},
zoey-kaiser marked this conversation as resolved.
Show resolved Hide resolved
isValidNumber(): boolean {
return isValidNumber(this.value);
},
Expand All @@ -43,6 +54,9 @@ export default Vue.extend({
},
methods: {
format(value: number | BigNumber): string {
if (this.formatted) {
return formatWithThousandSeparators(formatToAutomaticDecimalPoints(value, this.decimalPlaces));
}
return formatToAutomaticDecimalPoints(value, this.decimalPlaces);
},
},
Expand Down
8 changes: 7 additions & 1 deletion frontend/components/common/formatters/FormatCurrency.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<template>
<span>
<span v-if="isNotANumber">Unknown</span>
<span v-else>{{ sign }}<animated-number :value="value" :decimal-places="decimalPlaces" /> </span>
<span v-else
>{{ sign }}<animated-number :value="value" :decimal-places="decimalPlaces" :formatted="formatted" />
</span>
<span class="uppercase">{{ currency }}</span>
</span>
</template>
Expand Down Expand Up @@ -31,6 +33,10 @@ export default Vue.extend({
type: Number as Vue.PropType<Number>,
default: 2,
},
formatted: {
type: Boolean,
default: false,
},
},
computed: {
sign(): string {
Expand Down
11 changes: 7 additions & 4 deletions frontend/components/panels/VaultLiquidationLimitsCheckPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
The amount of DAI that will be auctioned after the current liquidation plus liquidation incentives
of this auction
</Explain>
= <FormatCurrency :value="globalDifference" currency="DAI"
= <FormatCurrency :value="globalDifference" currency="DAI" :formatted="true"
/></span>
<div v-else>
<span class="opacity-50">Unknown</span>
Expand Down Expand Up @@ -69,7 +69,7 @@
The amount of DAI that will be auctioned after the current liquidation plus liquidation incentives
of this auction
</Explain>
= <FormatCurrency :value="collateralDifference" currency="DAI"
= <FormatCurrency :value="collateralDifference" currency="DAI" :formatted="true"
/></span>
<div v-else>
<span class="opacity-50">Unknown</span>
Expand All @@ -93,7 +93,10 @@
import Vue from 'vue';
import BigNumber from 'bignumber.js';
import { VaultTransaction } from 'auctions-core/src/types';
import { formatToAutomaticDecimalPoints } from 'auctions-core/src/helpers/formatToAutomaticDecimalPoints';
import {
formatToAutomaticDecimalPoints,
formatWithThousandSeparators,
} from 'auctions-core/src/helpers/formatToAutomaticDecimalPoints';
import TextBlock from '~/components/common/other/TextBlock.vue';
import Explain from '~/components/common/other/Explain.vue';
import FormatCurrency from '~/components/common/formatters/FormatCurrency.vue';
Expand Down Expand Up @@ -180,7 +183,7 @@ export default Vue.extend({
},
methods: {
format(value: BigNumber): string {
return formatToAutomaticDecimalPoints(value);
return formatWithThousandSeparators(formatToAutomaticDecimalPoints(value));
zoey-kaiser marked this conversation as resolved.
Show resolved Hide resolved
},
},
});
Expand Down