gpt4 book ai didi

Javascript 原型(prototype)替换不起作用

转载 作者:行者123 更新时间:2023-12-02 17:38:07 24 4
gpt4 key购买 nike

这里我有以下String.prototype.replace调用;

$("#"+index+"stock").html(String.prototype.replace.call(value, /(\d{3})/, "$1" + (value%100000 > 0 ? 'K+' : 'K'), 'g'));

然后显示

910K+000

如何让它只显示

910K ?

原始917000可能值的列表如下

156167
785853
890000
162000
//any 100thousand number

这是此类问题的 JSFiddle:http://jsfiddle.net/2Q3yY/1/

最佳答案

为什么不简单地:

function formatNumber(num) {
var value = parseInt(num, 10);
if (value < 1000) return value;
if (value > 100000) {
value = value.toString().substr(0, 3) + 'K+';
} else {
value = value.toString().substr(0, 3) + 'K';
}
}

之后:

$("#"+index+"stock").html(formatNumber(value));

关于Javascript 原型(prototype)替换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22437840/

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