Skip to content

千分位分割 #5

@coffeexy

Description

@coffeexy

先记下别人写的正则:

let milliFormat = (num) => {  
    return num && num.toString()
        .replace(/^\d+/g, m => m.replace(/(?=(?!^)(\d{3})+$)/g, ','))
}

第一感觉是倒叙匹配

let milliFormat = (num) => {  
    return num && num.toString()
        .replace(/^\d+/g, m => {
          var array = [];
          for (i = m.length; i>3; ) {
            array.unshift(m.substr(i-3, 3));
            i = i - 3;
          }
          array.unshift(m.substring(0, i));
          return array.join(',');
        })
}

问陈日天,人说除1000啊

let milliFormat = (num) => {  
    return num && num.toString()
        .replace(/^\d+/g, m => {
          function me(v) {
            if (v<999) return v;
            return me(~~(v/1000)) + ',' + v%1000
          };
          return me(m);
        })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions