-1

I am trying to calculate a result and I want that in int.

calculation =>

   const amount = 2000 * 10 ** 18
    // Result I am Getiing is 2e+21
    
    console.log(amount);

I also tried the calculation in Math.floor(Number()) But still no success. Is there a way so I can make this calculation and get the real output in just numbers (like 2000000000000000000000) I need to pass this in an API and unfortunately that API parameter is int only.

Remark:- I am not looking for a BigInt solution

Mohit Chandel
  • 1,838
  • 10
  • 31
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt – Erenn Nov 29 '21 at 12:54
  • https://2ality.com/2012/07/large-integers.html this article might help – Gunal Bondre Nov 29 '21 at 12:57
  • @Erenn still getting the value `2e+21` after converting begint back to number – Mohit Chandel Nov 29 '21 at 12:57
  • 2
    Does this answer your question? [How to convert BigInt to Number in JavaScript?](https://stackoverflow.com/questions/53970655/how-to-convert-bigint-to-number-in-javascript) – Josh Adams Nov 29 '21 at 12:58
  • @JoshAdams Unfortunately not, I am not getting the correct number or int value – Mohit Chandel Nov 29 '21 at 13:02
  • This is not a good solution, but may can be a fast solution for you: amount.toLocaleString().split('.').join(""); – Ginés Ruiz Nov 29 '21 at 13:05
  • 2
    `2e+21` and `2000000000000000000000` are the same number. You do not want to convert it to a number, you are looking to convert the representation. Which can be done by changing it into a string. `amount.toString(10)` – 3limin4t0r Nov 29 '21 at 13:05
  • @3limin4t0r ok I got your point but I need to pass this in an API parameter and unfortunately, I only need int or number. – Mohit Chandel Nov 29 '21 at 13:07
  • @MohitChandel API parameter is still vague. Do you need to pass it as part of the URL? Is it request header value? Is it passed in the body? If passed in the body, which content type do you use? Do you pass JSON? If so you can just let [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) do the data encoding. Eg. `JSON.stringify(amount)`, this might produce the string `"2e+21"` but [this is valid JSON](https://www.json.org/img/number.png) and `JSON.parse("2e+21")` should produce the correct value on the receiving side. – 3limin4t0r Nov 29 '21 at 13:15
  • This is currently marked as a duplicate of a question about how to convert a BigInt to Number, when you know it is a safe integer. In this question, the asker does not have a BigInt to start with, does not want to convert it to a Number, and does not have a safe integer. The main problem here is confusion over `e+` scientific notation, and possibly rounding, two issues that are not present there. It's a total mismatch. – Jason Orendorff Nov 29 '21 at 14:27

1 Answers1

3

You can use BigInt.

// given amount comes from a library you have no control over
const amount = 2000 * 10 ** 18;

const bigInt = BigInt(amount);

// BigInt also has a toString utility method
console.log(bigInt.toString()); // 2000000000000000000000

// use BigInt directly when you are in control
const amount = 2000n * 10n ** 18n;

console.log(typeof amount);
console.log(amount.toString()); // 2000000000000000000000
adroste
  • 758
  • 1
  • 7
  • 17
  • But what if he need only int not string? – dev Nov 29 '21 at 13:03
  • 1
    Usually you'd want to compute large BigInt values like this: `2000n * 10n ** 10n`. That way you'll get an exact answer. Without the `n` you can get floating-point rounding. For example `11 ** 18` gets rounded (you can tell, the result is even!) but `11n ** 18n` is exact. – Jason Orendorff Nov 29 '21 at 13:04
  • @JasonOrendorff can you please make a code snippet of this? – Mohit Chandel Nov 29 '21 at 13:11
  • 1
    @JasonOrendorff you're right. My assumption was that he already has a large value that is a number/float (`amount` in the example above). But even then you can pack it in a BigInt and use the BigInt in your own controllable code. The toString method is great if it's just about displaying it in UI. – adroste Nov 29 '21 at 13:16