0
点赞
收藏
分享

微信扫一扫

探索Java设计模式:策略模式

橙子好吃吗 2024-04-21 阅读 14

问题

  • 数字转k、w结尾
    • 如:1000=1k 10000=1w

/**
 * 数字转k,w
 * @param {Number} num 
 * @returns String
 */
const numberTokw = num => {
 if (num < 1000) return num
    let endStr = 'w',
        numVal = 10000;

    if (num > 999 && num < 10000) {
        endStr = 'k'
        numVal = 1000;
    }
    let zhen = parseInt(num / numVal),
        yu = num % numVal;
    if (yu === 0) return zhen + endStr
    yu = String(yu).slice(0, 1)
    return zhen + '.' + yu + endStr
}

console.log(numberTokw(1000));
console.log(numberTokw(1100));
console.log(numberTokw(7000));
console.log(numberTokw(2363));
console.log(numberTokw(9999));
console.log(numberTokw(10000));
console.log(numberTokw(13400));
console.log(numberTokw(99999));

运行结果

在这里插入图片描述

举报

相关推荐

0 条评论