"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberExtensions = void 0;
exports.abbreviateNumber = abbreviateNumber;
exports.wrapInParentheses = wrapInParentheses;
exports.timesBy100 = timesBy100;
exports.divideBy100 = divideBy100;
function abbreviateNumber(numberToAbbreviate) {
    let str = '';
    if (numberToAbbreviate >= 1000000000) {
        str = (numberToAbbreviate / 1000000000).toFixed(1) + 'B';
    }
    else if (numberToAbbreviate >= 1000000) {
        str = (numberToAbbreviate / 1000000).toFixed(1) + 'M';
    }
    else if (numberToAbbreviate >= 1000) {
        str = (numberToAbbreviate / 1000).toFixed(1) + 'K';
    }
    else {
        str = numberToAbbreviate.toString();
    }
    return str;
}
function wrapInParentheses(numberToWrap) {
    return ' (' + numberToWrap + ')';
}
function timesBy100(value) {
    // the simple solution does not work
    // return value * 100;
    // needed because 0.12676 * 100 = 12.676000000000002
    // but we dont want that to happen
    // this solution does not handle  -0 correctly
    // also 1267.6 * 100 is 126759.99999999999
    // which is not what we want
    // return parseFloat((value * 100).toFixed(12));
    if (isNaN(value) || !value) {
        return value;
    }
    const valueAsStr = value.toPrecision();
    const separator = Number(1.1).toLocaleString()[1];
    const indexOfSeparator = valueAsStr.indexOf(separator);
    if (indexOfSeparator === -1) {
        return value * 100;
    }
    const [integer, decimal] = valueAsStr.split(separator);
    const num = [...`${integer}${decimal ?? ''}00`];
    num.splice(indexOfSeparator + 2, 0, separator);
    return Number(num.join(''));
}
function divideBy100(value) {
    // the simple solution does not work
    // return value / 100;
    // needed because 4012.3456/100 = 40.123456000000004
    // but we dont want that to happen
    // this solution does not handle  -0 correctly
    // return parseFloat((value / 100).toFixed(12));
    if (isNaN(value) || !value) {
        return value;
    }
    const isNegative = value < 0;
    if (isNegative) {
        value = -value;
    }
    const valueAsStr = value.toPrecision();
    const separator = Number(1.1).toLocaleString()[1];
    let indexOfSeparator = valueAsStr.indexOf(separator);
    if (indexOfSeparator === -1) {
        indexOfSeparator = 0;
    }
    let [integer = '', decimal] = valueAsStr.split(separator);
    decimal = decimal || '';
    integer = integer.padStart(2, '0');
    const integerArr = [...integer];
    const num = integerArr.slice(0, integerArr.length - 2).join('') +
        separator +
        integerArr.slice(-2).join('') +
        decimal;
    return Number(num) * (isNegative ? -1 : 1);
}
exports.NumberExtensions = {
    abbreviateNumber,
    wrapInParentheses,
    timesBy100,
    divideBy100,
};
exports.default = exports.NumberExtensions;
