How to get digits from a BigInt in javascript? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Adapting the Chinese Remainder Theorem (CRT) for integers to polynomials

Is there a verb for listening stealthily?

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

How can I list files in reverse time order by a command and pass them as arguments to another command?

How to make an animal which can only breed for a certain number of generations?

newbie Q : How to read an output file in one command line

Is this Kuo-toa homebrew race balanced?

What are some likely causes to domain member PC losing contact to domain controller?

Table formatting with tabularx?

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

Inverse square law not accurate for non-point masses?

An isoperimetric-type inequality inside a cube

"Destructive power" carried by a B-52?

Marquee sign letters

Where and when has Thucydides been studied?

Why do C and C++ allow the expression (int) + 4*5;

Is it OK to use the testing sample to compare algorithms?

What is "Lambda" in Heston's original paper on stochastic volatility models?

Can the Haste spell grant both a Beast Master ranger and their animal companion extra attacks?

Vertical ranges of Column Plots in 12

New Order #6: Easter Egg

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

Does the main washing effect of soap come from foam?

Flight departed from the gate 5 min before scheduled departure time. Refund options



How to get digits from a BigInt in javascript?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I am working on problem n°104 of project Euler Problem 104 and would like to do it in javascript.



In order to solve this problem I need to compute large values of the Fibonacci sequence, but the numbers produced by this sequence are too large to be handle by classic Number, so I'm using BigInt supported in the latest versions of javascript.



Once I've got a particular result stored in a BigInt, I need to check it's 10 first, and last digits.




To get the digits from a Number we usually do something like in the code below, but when the number becomes very large, things go wrong:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result





It seems like the "toString()" methods is only using the precision of the Number type (2^53 I believe), thus we are quickly losing precision on the last digits of the BigInt number. The problem is I can't find other methods to extract those digits.



Edit :
I need the precision to be perfect because basicaly what i'm doing for example is :



Compute Fibonacci(500) = 280571172992510140037611932413038677189525



Get the 10 last digits of this number : 8677189525 (this is where is lose the precision)



And then to solve my problem I need to check that those 10 last digits contains all the digits from 1 to 9










share|improve this question
























  • Exactly how precise do you want the numbers to be? Questions typically get better answers when you explicitly state what your desired results are. Also, +1

    – Caleb Goodman
    Mar 9 at 0:49

















3















I am working on problem n°104 of project Euler Problem 104 and would like to do it in javascript.



In order to solve this problem I need to compute large values of the Fibonacci sequence, but the numbers produced by this sequence are too large to be handle by classic Number, so I'm using BigInt supported in the latest versions of javascript.



Once I've got a particular result stored in a BigInt, I need to check it's 10 first, and last digits.




To get the digits from a Number we usually do something like in the code below, but when the number becomes very large, things go wrong:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result





It seems like the "toString()" methods is only using the precision of the Number type (2^53 I believe), thus we are quickly losing precision on the last digits of the BigInt number. The problem is I can't find other methods to extract those digits.



Edit :
I need the precision to be perfect because basicaly what i'm doing for example is :



Compute Fibonacci(500) = 280571172992510140037611932413038677189525



Get the 10 last digits of this number : 8677189525 (this is where is lose the precision)



And then to solve my problem I need to check that those 10 last digits contains all the digits from 1 to 9










share|improve this question
























  • Exactly how precise do you want the numbers to be? Questions typically get better answers when you explicitly state what your desired results are. Also, +1

    – Caleb Goodman
    Mar 9 at 0:49













3












3








3








I am working on problem n°104 of project Euler Problem 104 and would like to do it in javascript.



In order to solve this problem I need to compute large values of the Fibonacci sequence, but the numbers produced by this sequence are too large to be handle by classic Number, so I'm using BigInt supported in the latest versions of javascript.



Once I've got a particular result stored in a BigInt, I need to check it's 10 first, and last digits.




To get the digits from a Number we usually do something like in the code below, but when the number becomes very large, things go wrong:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result





It seems like the "toString()" methods is only using the precision of the Number type (2^53 I believe), thus we are quickly losing precision on the last digits of the BigInt number. The problem is I can't find other methods to extract those digits.



Edit :
I need the precision to be perfect because basicaly what i'm doing for example is :



Compute Fibonacci(500) = 280571172992510140037611932413038677189525



Get the 10 last digits of this number : 8677189525 (this is where is lose the precision)



And then to solve my problem I need to check that those 10 last digits contains all the digits from 1 to 9










share|improve this question
















I am working on problem n°104 of project Euler Problem 104 and would like to do it in javascript.



In order to solve this problem I need to compute large values of the Fibonacci sequence, but the numbers produced by this sequence are too large to be handle by classic Number, so I'm using BigInt supported in the latest versions of javascript.



Once I've got a particular result stored in a BigInt, I need to check it's 10 first, and last digits.




To get the digits from a Number we usually do something like in the code below, but when the number becomes very large, things go wrong:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result





It seems like the "toString()" methods is only using the precision of the Number type (2^53 I believe), thus we are quickly losing precision on the last digits of the BigInt number. The problem is I can't find other methods to extract those digits.



Edit :
I need the precision to be perfect because basicaly what i'm doing for example is :



Compute Fibonacci(500) = 280571172992510140037611932413038677189525



Get the 10 last digits of this number : 8677189525 (this is where is lose the precision)



And then to solve my problem I need to check that those 10 last digits contains all the digits from 1 to 9






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result





let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = BigInt(1234567891111111111111111111111111111)
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // unpredictable result






javascript bigint






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 0:56







S. Sylvain

















asked Mar 9 at 0:44









S. SylvainS. Sylvain

164




164












  • Exactly how precise do you want the numbers to be? Questions typically get better answers when you explicitly state what your desired results are. Also, +1

    – Caleb Goodman
    Mar 9 at 0:49

















  • Exactly how precise do you want the numbers to be? Questions typically get better answers when you explicitly state what your desired results are. Also, +1

    – Caleb Goodman
    Mar 9 at 0:49
















Exactly how precise do you want the numbers to be? Questions typically get better answers when you explicitly state what your desired results are. Also, +1

– Caleb Goodman
Mar 9 at 0:49





Exactly how precise do you want the numbers to be? Questions typically get better answers when you explicitly state what your desired results are. Also, +1

– Caleb Goodman
Mar 9 at 0:49












1 Answer
1






active

oldest

votes


















3














For big numbers, I think you should add the n suffix:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result








share|improve this answer























  • Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

    – S. Sylvain
    Mar 9 at 1:02











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072887%2fhow-to-get-digits-from-a-bigint-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














For big numbers, I think you should add the n suffix:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result








share|improve this answer























  • Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

    – S. Sylvain
    Mar 9 at 1:02















3














For big numbers, I think you should add the n suffix:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result








share|improve this answer























  • Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

    – S. Sylvain
    Mar 9 at 1:02













3












3








3







For big numbers, I think you should add the n suffix:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result








share|improve this answer













For big numbers, I think you should add the n suffix:






let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result








let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result





let number = BigInt(123456789)
console.log(number.toString())
console.log(number.toString()[3]) // Result is fine

let bigNumber = 1234567891111111111111111111111111111n // <-- n suffix, literal syntax
console.log(bigNumber.toString())
console.log(bigNumber.toString()[30]) // result

let bigNumber2 = BigInt('1234567891111111111111111111111111111') // <-- also works as a string, in case you can't use the literal for some reason
console.log(bigNumber2.toString())
console.log(bigNumber2.toString()[30]) // result






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 at 0:50









Austin GrecoAustin Greco

21.8k44249




21.8k44249












  • Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

    – S. Sylvain
    Mar 9 at 1:02

















  • Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

    – S. Sylvain
    Mar 9 at 1:02
















Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

– S. Sylvain
Mar 9 at 1:02





Thank you, it's quite tricky that we can't use the BigInt() initializer without using n or literal but I guess it makes sense

– S. Sylvain
Mar 9 at 1:02



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072887%2fhow-to-get-digits-from-a-bigint-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко