gpt4 book ai didi

javascript - 如果数字前面有减号,如何将其格式化为 Lac 或 Crores

转载 作者:行者123 更新时间:2023-11-28 14:54:21 25 4
gpt4 key购买 nike

将数字转换为 lakhs and crores仅适用于正数。
尝试转换为字符串并返回,但这对我来说不起作用。
我还需要转换负数,例如-330000 应转换为 -3.30 Lac

function numDifferentiation(val) {
if (val >= 10000000) {
val = (val / 10000000).toFixed(2) + ' Cr';
} else if (val >= 100000) {
val = (val / 100000).toFixed(2) + ' Lac';
}
/*else if(val >= 1000) val = (val/1000).toFixed(2) + ' K';*/
return val;
}
console.log(numDifferentiation(NaN))
console.log(numDifferentiation(0))

console.log(numDifferentiation(1))
console.log(numDifferentiation(100))
console.log(numDifferentiation(1000))
console.log(numDifferentiation(100000))
console.log(numDifferentiation(10000000))

console.log(numDifferentiation(-1))
console.log(numDifferentiation(-100))
console.log(numDifferentiation(-1000))
console.log(numDifferentiation(-100000))
console.log(numDifferentiation(-10000000))
.as-console-wrapper {
/* Make stack overflow console fullscreen */
max-height: 100% !important;
top: 0;
}

最佳答案

您可以使用Math.abs()返回数字的绝对值:

function numDifferentiation(value) {
const val = Math.abs(value)
if (val >= 10000000) return `${(value / 10000000).toFixed(2)} Cr`
if (val >= 100000) return `${(value / 100000).toFixed(2)} Lac`
return value;
}
console.log(numDifferentiation(NaN))
console.log(numDifferentiation(0))

console.log(numDifferentiation(1))
console.log(numDifferentiation(100))
console.log(numDifferentiation(1000))
console.log(numDifferentiation(100000))
console.log(numDifferentiation(10000000))

console.log(numDifferentiation(-1))
console.log(numDifferentiation(-100))
console.log(numDifferentiation(-1000))
console.log(numDifferentiation(-100000))
console.log(numDifferentiation(-10000000))
.as-console-wrapper {
/* Make stack overflow console fullscreen */
max-height: 100% !important;
top: 0;
}

关于javascript - 如果数字前面有减号,如何将其格式化为 Lac 或 Crores,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43465219/

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