河畔的风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

toFixed()方法采用1 个参数,即精确的小数位数。

let num = 12.345678; // default toFixed() method

console.log(num.toFixed()); // 12

 

逗号分隔的内置方法

这些格式可以通过使用toLocaleString()方法来实现。

 

let num = 7323452568.283;

// US system en-US

console.log(num.toLocaleString('en-US')); // 7,323,452,568.283

// India system hi-IN

console.log(num.toLocaleString('hi-IN')); // 7,32,34,52,568.283

 

逗号分隔的自定义函数

function separateComma(val) {
    // remove sign if negative
    var sign = 1;
    if (val < 0) {
        sign = -1;
        val = -val;
    }
    // trim the number decimal point if it exists
    let num = val.toString().includes('.') ? val.toString().split('.')[0] : val.toString();
    let len = num.toString().length;
    let result = '';
    let count = 1;

    for (let i = len - 1i >= 0i--) {
        result = num.toString()[i] + result;
        if (count % 3 === 0 && count !== 0 && i !== 0) {
            result = ',' + result;
        }
        count++;
    }

    // add number after decimal point
    if (val.toString().includes('.')) {
        result = result + '.' + val.toString().split('.')[1];
    }
    // return result with - sign if negative
    return sign < 0 ? '-' + result : result;
}

let num1 = 12345678;
console.log(separateComma(num1));

// decimal number
let num2 = -723694769.2343;
console.log(separateComma(num2));

正则

function commaSeparateNumber(val) {
    // remove sign if negative
    var sign = 1;
    if (val < 0) {
        sign = -1;
        val = -val;
    }

    // trim the number decimal point if it exists
    let num = val.toString().includes('.') ? val.toString().split('.')[0] : val.toString();

    while (/(\d+)(\d{3})/.test(num.toString())) {
        // insert comma to 4th last position to the match number
        num = num.toString().replace(/(\d+)(\d{3})/'$1' + ',' + '$2');
    }

    // add number after decimal point
    if (val.toString().includes('.')) {
        num = num + '.' + val.toString().split('.')[1];
    }

    // return result with - sign if negative
    return sign < 0 ? '-' + num : num;
}
// decimal number
let num1 = 77799;
console.log(commaSeparateNumber(num1));

// decimal number
let num2 = -72364769.1234;
console.log(commaSeparateNumber(num2));

 

functioncommaSeparateNumber(val){// remove sign if negativevar sign =1;if(val <0){ sign =-1; val =-val;}// trim the number decimal point if it existslet num = val.toString().includes('.')? val.toString().split('.')[0]: val.toString();while(/(\d+)(\d{3})/.test(num.toString())){// insert comma to 4th last position to the match number num = num.toString().replace(/(\d+)(\d{3})/,'$1'+','+'$2');}// add number after decimal pointif(val.toString().includes('.')){ num = num +'.'+ val.toString().split('.')[1];}// return result with - sign if negativereturn sign <0?'-'+ num : num;}// decimal numberlet num1 =77799; console.log(commaSeparateNumber(num1));// decimal numberlet num2 =-72364769.1234; console.log(commaSeparateNumber(num2));
posted on 2022-05-10 18:09  河畔的风  阅读(17)  评论(0编辑  收藏  举报