gpt4 book ai didi

javascript - 如何格式化类似于 Stack Overflow 信誉格式的数字

转载 作者:可可西里 更新时间:2023-11-01 01:45:50 26 4
gpt4 key购买 nike

我想将数字转换为字符串表示形式,其格式类似于 Stack Overflow 信誉显示。

例如

  • 999 == '999'
  • 1000 == '1,000'
  • 9999 == '9,999'
  • 10000 == '10k'
  • 10100 == '10.1k'

最佳答案

另一种完全产生所需输出的方法:

function getRepString (rep) {
rep = rep+''; // coerce to string
if (rep < 1000) {
return rep; // return the same number
}
if (rep < 10000) { // place a comma between
return rep.charAt(0) + ',' + rep.substring(1);
}
// divide and format
return (rep/1000).toFixed(rep % 1000 != 0)+'k';
}

查看输出结果here .

关于javascript - 如何格式化类似于 Stack Overflow 信誉格式的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3177855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com