Number converted to 1e + 30
The main library doesn't give you support for numbers that don't fit into the native type number
, so you probably want to use a third party library to help you with big decimal places.
For example, https://mikemcl.github.io/decimal.js/
new Decimal('1e+30').toFixed()
// "1000000000000000000000000000000"
source to share
You can use new Array()
and String.replace
, but it will only be in the formString
function toNum(n) {
var nStr = (n + "");
if(nStr.indexOf(".") > -1)
nStr = nStr.replace(".","").replace(/\d+$/, function(m){ return --m; });
return nStr.replace(/(\d+)e\+?(\d+)/, function(m, g1, g2){
return g1 + new Array(+g2).join("0") + "0";
})
}
console.log(toNum(1e+30)); // "1000000000000000000000000000000"
It is now more reliable as it does not crash even if a really huge amount, for example 12e100
, to be converted to 1.2e+101
, is provided when removed .
, and the last set of digits was decremented once, But 100% accuracy cannot be ensured, but this is due to limitations of math computing in javascript.
source to share
You can use toLocaleString
(1000000000000000000000000000000).toLocaleString("en-US", { useGrouping: false })
source to share