@stokr/components-library
Version:
STOKR - Components Library
48 lines (46 loc) • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.km_ify = void 0;
const km_ify = function (input, decimals) {
let convertBillion = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (input >= 1000000000 && convertBillion) {
input /= 1000000000;
if (input % 10 !== 0) {
input = truncateDecimals(input, 1);
}
return input += 'B';
}
if (input >= 1000000) {
input /= 1000000;
if (input % 10 !== 0) {
input = truncateDecimals(input, 1);
}
return input += 'M';
}
if (input >= 1000) {
input /= 1000;
if (input % 10 !== 0) {
input = truncateDecimals(input, 1);
}
return input += 'K';
}
if (decimals) {
return input.toFixed(decimals);
} else return input;
};
exports.km_ify = km_ify;
const truncateDecimals = (number, digits) => {
var re = new RegExp('(\\d+\\.\\d{' + digits + '})(\\d)'),
m = number.toString().match(re);
return m ? parseFloat(m[1]) : number.valueOf();
};
//how the number should appear - SKR-870
// 1-999 just the number
// 1000 1K
// 1001 - <1100 1K
// 1100 - <1200 1.1K
// 999900 -999999 999.9K
// 1000000 - <1100000 1M
// 1100000 -<1200000 1.1M