[英]Most efficient code to add a dollar sign behind a negative/positive number in JS
I have two prices, A
and B
, and I want to show A - B
with a dollar sign.我有两个价格,
A
和B
,我想用美元符号显示A - B
。
A: 10, B: 30 => -$20
A: 40, B: 5 => $35
It is possible to use this function:可以使用这个 function:
function minus(A, B) {
return `${A-B < 0 && "-"}$${Math.abs(A-B)}`
}
I'am just wondering if it's the most efficient way (in terms of speed & performance) to do this, or there's a wiser (.) (and maybe faster) approach.我只是想知道这是否是执行此操作的最有效方法(就速度和性能而言),或者是否有更明智的 (.)(也许更快)方法。
I didnt check if it is faster, but i prefer this approach:我没有检查它是否更快,但我更喜欢这种方法:
(a, b) => String(a - b).replace(/^-?/, '$&\$')
just a matter of taste:)只是品味问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.