没有科学记数法将大数字转换为字符串

时间:2013-11-15 12:41:03

标签: javascript

例如Number.MAX_VALUE.toString()"1.7976931348623157e+308"

我希望没有e+308,如何实现这个目标?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var n = Number.MAX_VALUE.toString();
var parts = n.split("e+");
var first = parts[0].replace('.', "");
var zeroes = parseInt(parts[1], 10) - (first.length - 1);
for(var i = 0; i < zeroes; i++){ first += "0"; }
// => first === "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"