当前位置 : 主页 > 网络编程 > JavaScript >

字符串浮点数小数位截位

来源:互联网 收集:自由互联 发布时间:2021-06-30
number.js function isLegalFloat(origin, scale) { // 判断是否是数字 if (origin === null || isNaN(origin)) { return false; } // 准备验证数字格式的正则表达式 var regExp = null; if (scale 1) { regExp = /(^-?[1-9]{1}\d{0,9}$
number.js
function isLegalFloat(origin, scale) {

    // 判断是否是数字
    if (origin === null || isNaN(origin)) {
        return false;
    }

    // 准备验证数字格式的正则表达式
    var regExp = null;
    if (scale < 1) {
        regExp = /(^-?[1-9]{1}\d{0,9}$)|(^0$)/;
    } else {
        var expression = "(^-?[1-9]{1}\\d{0,9}(\\.\\d{1,scale})?$)|(^-?0\\.\\d{1,scale}$)|(^-?[1-9]{1}\\d{0,9}$)|(^0$)";
        expression = expression.replace(/scale/g, scale);
        regExp = new RegExp(expression);
    }
    return regExp.test(origin);
}

function formatFloat(origin, scale) {
    if(typeof origin == "number"){
        origin = origin.toString();
    }
    // 排除非法数字字符串
    if (origin === null || isNaN(origin) || origin.length == 0) {
        return {
            legal: false,
            value: ""
        }
    }

    // 整理符号位
    var firstcharacter = origin[0];
    var hasSign = (firstcharacter == '-' || firstcharacter == '+')
    var isNegative = (firstcharacter == '-');
    if (hasSign) {
        origin = origin.substr(1);
        console.log("有符号位");
        console.log(origin);
    }

    var dotIndex = origin.indexOf('.');
    var value = null;
    if (dotIndex == -1 || dotIndex == (origin.length - 1)) {
        /**
         * origin like
         * xxx.
         * xxx
         * 截取小数点scale位小数
         */
        origin = origin.replace(/\./g, '');
        /**
         * origin like
         * xxx. → xxx
         * xxx
         * 截取小数点scale位小数
         */
        if (origin == 0) {
            value = "0"
        } else {
            /**
             * origin like
             * 0+[1-9]\d*
             * 截取小数点scale位小数
             */
            value = (hasSign ? "-" : "") + origin.replace(/^0+/, '');
            /**
             * value like
             * [1-9]\d*
             * 截取小数点scale位小数
             */
        }
    } else if (dotIndex == 0) {
        /**
         * origin like
         * .xxxxxxxxxx
         * .xxx
         * 截取小数点scale位小数
         */
        if(scale == 0){
            return {
                legal: false,
                value: ""
            }
        }
        if (origin.length - 1 > scale) {
            origin = origin.substr(0, scale + 1);
        }
        value = (hasSign ? "-" : "") + "0" + origin;
    } else {
        var leftPart = origin.substr(0, dotIndex);
        if (leftPart == 0) {
            leftPart = "0";
        } else {
            leftPart = leftPart.replace(/^0+/, '');
        }

        var rightPart = origin.substr(dotIndex);
        if (rightPart.length - 1 > scale) {
            rightPart = rightPart.substr(0, scale + 1)
        }
        value = (hasSign ? "-" : "") + leftPart + (scale == 0 ? "" : rightPart);
    }
    return {
        legal: true,
        value: value
    };
}
网友评论