如何将BigInt或表示数字的长字符串转换为科学计数法

问题描述 投票:1回答:1

我在BigInt中进行了大量计算。如何将其转换为科学计数法?

var max = Number.MAX_SAFE_INTEGER;
console.log(max.toExponential(3).toString());

var big = BigInt(max.toString()+max.toString());
console.log(big.toExponential(3).toString())
javascript scientific-notation
1个回答
1
投票

可以将Intl.NumberFormat{notation: 'scientific'}一起使用

var max = Number.MAX_SAFE_INTEGER;

var big = BigInt(max.toString()+max.toString());

var formatter = new Intl.NumberFormat('en', {
  notation: 'scientific'
});
  
console.log(formatter.format(big));

BigIntnotation: scientific两者在此回答日期之前都几乎不受浏览器的支持。如果您打算使用跨浏览器,请考虑checking their support

© www.soinside.com 2019 - 2024. All rights reserved.