Number converted to 1e + 30

How to convert 1e+30

to 1000000000000000000000000000000

I want the number entered by the user to not convert as 1e+30

.

How can this be achieved? Is there a way to display the actual digits after parsing to float or int ?

+3


source to share


3 answers


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"

      

+5


source


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.

+3


source


You can use toLocaleString

(1000000000000000000000000000000).toLocaleString("en-US", { useGrouping: false })

      

+3


source







All Articles