[英]Need to convert number to currency
Currently using below code for conversion of number to currency.目前使用以下代码将数字转换为货币。 The only issue is if I have 1000 it is giving 1000 instead I need 1k.
唯一的问题是如果我有 1000,它会给出 1000 而不是我需要 1k。
Current implementation 1000 - 1000
当前实施
1000 - 1000
Need 1000 - 1k
需要
1000 - 1k
Tried in lot many ways to get it done but unable to resolve.尝试了很多方法来完成它,但无法解决。
var number = 12345678910;
var digits = 2;
var suffix = ["", "K.", "M.", "B."];
var nbDigits = parseInt(Math.log(number)/Math.LN10);
var power = nbDigits - nbDigits%3;
var tmp = number/ Math.pow(10, power);
var suffixIndex = Math.min(3, power/3);
var result = "$" + tmp.toFixed(digits) + " " + suffix[suffixIndex];
Just simplify calculation of number of digits:只需简化位数的计算:
// From:
var nbDigits = parseInt(Math.log(number)/Math.LN10);
// To:
var nbDigits1 = Math.log10(number);
That'll give you the number of digits, without rounding errors.这将为您提供位数,而不会出现四舍五入错误。 It does return
$1.00 K.
for 1000
.它确实为
1000
返回了$1.00 K.
.。
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.