河畔的风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  61 随笔 :: 0 文章 :: 0 评论 :: 15287 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

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   河畔的风  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示