Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: toString(36) for large integers

15955067621307336078.toString(36); returns '3d7vzfy5k2as8' in Javascript because the large integer cannot be represented (the correct answer is '3d7vzfy5k29ou').

Does someone have a clever function that takes a large integer as a string and converts it to base 36?

like image 253
Meekohi Avatar asked Nov 26 '12 21:11

Meekohi


People also ask

What is toString 36 in JavaScript?

This parameter specifies the base in which the integer is represented in the string. It is an integer between 2 and 36 which is used to specify the base for representing numeric values. Return Value: The num. toString() method returns a string representing the specified number object.

What is toString 16 in JS?

Therefore, toString(16) converts a given variable into a String in a desired form, in this case you want it to be in the hexadecimal form.

How do I convert a number to a string in JavaScript?

JavaScript Number toString() The toString() returns a number as a string.

How many parameters are required in the toString () method?

toString(int a) is an inbuilt method in Java which is used to return a String object, representing the specified integer in the parameter. Parameters: The method accepts one parameter a of integer type and refers to the integer needed to be converted to string.


2 Answers

Use this BigInt class, which allows conversion of arbitrarily large integers to any arbitrary base between 2 and 95. Use the bigInt2str() method to perform the conversion.

like image 140
Robert Harvey Avatar answered Oct 02 '22 13:10

Robert Harvey


For anyone curious, today we are using https://github.com/MikeMcl/bignumber.js instead.

like image 33
Meekohi Avatar answered Oct 02 '22 11:10

Meekohi