Number converted in 1e+30
Asked Answered
D

4

7

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?

Disaccharide answered 19/12, 2014 at 8:3 Comment(3)
what about 1e30 ? :)Trichinopoly
You do realize that javascript cannot handle numbers of 30 digits right?Kidwell
@RokoC.Buljan It's my requirement to remain in actual digits.Disaccharide
B
5

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, https://mikemcl.github.io/decimal.js/

new Decimal('1e+30').toFixed()
// "1000000000000000000000000000000"
Bluestone answered 19/12, 2014 at 8:17 Comment(4)
I think you mean core language. ;-)Anaximenes
@Anaximenes Meh. I know that's the terminology that JS uses, but it's goofy.Bluestone
Please give me example I stuck by this problem.Disaccharide
@Disaccharide My answer already contains an example. What more are you asking?Bluestone
R
3

You may use toLocaleString

(1000000000000000000000000000000).toLocaleString("en-US", { useGrouping: false })
Rancell answered 19/12, 2014 at 10:28 Comment(2)
@Disaccharide It works with float numbers (in JS there is no difference between integers and floats). However, with so huge numbers you can't have decimal precision (in fact, 1000000000000000000000000000000 == 1000000000000000010000000000000).Affright
This should be the answer!It works perfectly and is very simple.Microfarad
F
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.

Farouche answered 19/12, 2014 at 8:10 Comment(4)
@Disaccharide It is working. First it is getting converted to 1e+28 and it's working fine.Farouche
parseFloat((10000000000000000000000000000.55) is eqaul to 1e+28Farouche
It display only 10000000000000000000000000000Disaccharide
Can you show me in jsfiddle example which you say it's working fineDisaccharide
S
0

JavaScript's Number type is based on the IEEE 754 double-precision floating-point format, which cannot accurately represent integers larger than 2e53 − 1.

For handling large integers, you should use BigInt

console.log(BigInt("1000000000000000000000000000000") // 1000000000000000000000000000000n

console.log(String(BigInt("1000000000000000000000000000000"))) // "1000000000000000000000000000000"

In the first log, n at the end indicates that it's a BigInt, nothing more

Stinkstone answered 27/9 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.