reduce precision for larger numbers

This commit is contained in:
Sean Yesmunt 2019-10-02 00:10:19 -04:00
parent b998637435
commit d770011057
2 changed files with 48 additions and 20 deletions

32
dist/bundle.es.js vendored
View file

@ -1874,21 +1874,33 @@ function numberWithCommas(x) {
}
function formatCredits(amount, precision, shortFormat = false) {
let actualAmount = parseFloat(amount),
suffix = '';
let actualAmount = parseFloat(amount);
let actualPrecision = parseFloat(precision);
let suffix = '';
if (Number.isNaN(actualAmount)) return '0';
if (shortFormat) {
if (actualAmount >= 1000000 && precision <= 7) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
} else if (actualAmount >= 1000 && precision <= 4) {
actualAmount = actualAmount / 1000;
suffix = 'K';
if (actualAmount >= 1000000) {
if (precision <= 7) {
if (shortFormat) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
} else {
actualPrecision -= 7;
}
}
} else if (actualAmount >= 1000) {
if (precision <= 4) {
if (shortFormat) {
actualAmount = actualAmount / 1000;
suffix = 'K';
} else {
actualPrecision -= 4;
}
}
}
return numberWithCommas(actualAmount.toFixed(precision || 1).replace(/\.?0+$/, '')) + suffix;
return numberWithCommas(actualAmount.toFixed(actualPrecision >= 0 ? actualPrecision : 1).replace(/\..*0+$/, '')) + suffix;
}
function formatFullPrice(amount, precision = 1) {

View file

@ -5,21 +5,37 @@ function numberWithCommas(x) {
}
export function formatCredits(amount, precision, shortFormat = false) {
let actualAmount = parseFloat(amount),
suffix = '';
let actualAmount = parseFloat(amount);
let actualPrecision = parseFloat(precision);
let suffix = '';
if (Number.isNaN(actualAmount)) return '0';
if (shortFormat) {
if (actualAmount >= 1000000 && precision <= 7) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
} else if (actualAmount >= 1000 && precision <= 4) {
actualAmount = actualAmount / 1000;
suffix = 'K';
if (actualAmount >= 1000000) {
if (precision <= 7) {
if (shortFormat) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
} else {
actualPrecision -= 7;
}
}
} else if (actualAmount >= 1000) {
if (precision <= 4) {
if (shortFormat) {
actualAmount = actualAmount / 1000;
suffix = 'K';
} else {
actualPrecision -= 4;
}
}
}
return numberWithCommas(actualAmount.toFixed(precision || 1).replace(/\.?0+$/, '')) + suffix;
return (
numberWithCommas(
actualAmount.toFixed(actualPrecision >= 0 ? actualPrecision : 1).replace(/\..*0+$/, '')
) + suffix
);
}
export function formatFullPrice(amount, precision = 1) {