Number Converted in 1E+30

How to convert 1e+30 to 1000000000000000000000000000000

I want number as it is entered by User do not convert like 1e+30.

How can achieve this? Is there any way to display actual digits after parse it to float or int?

3

3 Answers

The core library doesn't give you any support for numbers that don't fit into the native number type, so you'll probably want to use a third party library to help you with large decimals.

For example,

new Decimal('1e+30').toFixed()
// "1000000000000000000000000000000"
4

You may use toLocaleString

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

You can make use of new Array() and String.replace, but it will only be in the form of String

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"

Now it's more robust as it doesn't fail even if a really huge number such as 12e100 which will be converted to 1.2e+101, is provided as the . is removed and the last set of digits decremented once. But still 100% accuracy can't be ensured but that is because of limitations of floatation maths in javascript.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest