var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Injectable } from '@angular/core';
let FractionDigitService = class FractionDigitService {
    constructor() {
        this.zeroText = '0';
    }
    /**
     * 左补齐字符串
     * number  要处理的数字
     * fillLength 要补齐的长度
     * char 要补齐的字符
     */
    padLeft(number, fillLength, char) {
        let result = String(number);
        char = char ? char : this.zeroText; // 默认补0
        let currentLength = result.length;
        while (currentLength < fillLength) {
            result = char + result;
            currentLength++;
        }
        return result.toString();
    }
    /**
     * 右补齐字符串
     * @param number  要处理的数字
     * @param fillLength 要补齐的长度
     * @param char  要补齐的字符
     */
    padRight(number, fillLength, char) {
        let result = String(number);
        char = char ? char : this.zeroText; // 默认补0
        let currentLength = result.length;
        while (currentLength < fillLength) {
            result = result + char;
            currentLength++;
        }
        return result.toString();
    }
    /**
     * 左移小数点位置（用于数学计算，相当于除以Math.pow(10,scale)）
     * @param fractionDigits 要移位的刻度
     * @return
     */
    movePointLeft(number, fractionDigits) {
        if (fractionDigits <= 0) {
            return number;
        }
        const numberText = String(number);
        const decimalParts = numberText.split('.');
        let intergerPart = decimalParts[0] ? decimalParts[0] : '';
        const fractionPart = decimalParts[1] ? decimalParts[1] : '';
        let fractionPointText = '.';
        let minusText = '';
        if (intergerPart.slice(0, 1) === '-') {
            intergerPart = intergerPart.slice(1);
            minusText = '-';
        }
        if (intergerPart.length <= fractionDigits) {
            fractionPointText = '0.';
            intergerPart = this.padLeft(intergerPart, fractionDigits, '0');
        }
        return minusText +
            intergerPart.slice(0, -fractionDigits) +
            fractionPointText +
            intergerPart.slice(-fractionDigits) +
            fractionPart;
    }
    /**
     * 右移小数点位置（用于数学计算，相当于乘以Math.pow(10,scale)）
     * @param number  要处理的数字
     * @param fractionDigits 要移位的刻度
     */
    movePointRight(number, fractionDigits) {
        if (fractionDigits <= 0) {
            return number;
        }
        // eslint-disable-next-line no-new-wrappers
        const numberText = new String(number);
        const decimalParts = numberText.split('.');
        const intergerPart = decimalParts[0] ? decimalParts[0] : '';
        let fractionPart = decimalParts[1] ? decimalParts[1] : '';
        let fractionPointText = '.';
        if (fractionPart.length <= fractionDigits) {
            fractionPointText = '';
            fractionPart = this.padRight(fractionPart, fractionDigits, '0');
        }
        return intergerPart +
            fractionPart.slice(0, fractionDigits) +
            fractionPointText +
            fractionPart.slice(fractionDigits, fractionPart.length);
    }
    /**
     * 移动小数点位置（用于数学计算，相当于（乘以/除以）Math.pow(10,scale)）
     * @param number  要处理的数字
     * @param fractionDigits  要移位的刻度（正数表示向右移；负数表示向左移动；0返回原值）
     */
    movePoint(number, fractionDigits) {
        return fractionDigits >= 0
            ? this.movePointRight(number, fractionDigits)
            : this.movePointLeft(number, -fractionDigits);
    }
    /**
     * 按照指定的小数位，对数字进行四舍五入计算
     * @param number 要处理的数字
     * @param fractionDigits 指定的小数位
     */
    roundingToFixedFractionDigits(number, fractionDigits) {
        const numberText = String(number);
        const fractionPointIndex = numberText.indexOf('.') + 1;
        const currentFractionDigits = numberText.length - fractionPointIndex;
        if (fractionPointIndex >= 1 && currentFractionDigits > fractionDigits) {
            let result = +this.movePoint(numberText, fractionDigits);
            const roundingNumber = Number(numberText.substr(fractionPointIndex + fractionDigits, 1));
            if (roundingNumber >= 5 && number >= 0 || roundingNumber < 5 && number < 0) {
                result = Math.ceil(result);
            }
            else {
                result = Math.floor(result);
            }
            return this.movePoint(result, -fractionDigits);
        }
        return Number(number).toFixed(fractionDigits);
    }
};
FractionDigitService = __decorate([
    Injectable()
], FractionDigitService);
export { FractionDigitService };
