react 限制小数点位数

时间:2021-02-05 10:41:28   收藏:0   阅读:0
limitDecimals = value => {
        const reg = /^(\-)*(\d+)\.(\d).*$/;
        if (typeof value === ‘string‘) {
            return !isNaN(Number(value)) ? value.replace(reg, ‘$1$2.$3‘) : ‘‘;
        } else if (typeof value === ‘number‘) {
            return !isNaN(value) ? String(value).replace(reg, ‘$1$2.$3‘) : ‘‘;
        } else {
            return ‘‘;
        }
    };

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!