Skip to content

实现千分位 #8

@AILINGANGEL

Description

@AILINGANGEL

用正则表达式好麻烦,直接来算法呗....

function formatMoney(money) {
    let i = money.length - 1;
    let tag = 0;
    let ans = [];
    while (i > -1) {
        tag++;
        ans.unshift(money[i]);
        if (tag === 3 && i !== 0) { // i不能等于0是因为如果是100,000这种格式不要在100前面再加逗号了
            ans.unshift(',');
            tag = 0;
        }
        i--;
    }
    return ans.join('');
}

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