-1

I have one API for generate a random map tileset with a Erlang server, with safe integer JavaScript can request to API good but the problem is when is more than Number.MAX_SAFE_INTEGER.

Then, in the server I haven't the integer becuse JavaScript send 1e+21 number in scientific notation, not 999999999999999999999 and I need the number like string not in scientific notation.

How can I get a string like "999999999999999999999" in JavaScript for send to API that and not scientific notation string? Exists a library for arbitrary long numbers in JavaScript small only for the number? The umber is a X and Y number of position and need the correct position, and not is good a big library because need good performance for render the map in ms, is for a game in browser, not for astronomy calc.

  • 1
    check this: http://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript – O_Z Sep 24 '16 at 21:33
  • `1e21` in JavaScript _is_ a number that corresponds to that notation. It will fail if you try to convert it to a string, e.g. `""+ 1e21 // "1e+21"` however the _number_ itself is only displayed in scientific notation, doesn't literally have the `e` there – VLAZ Sep 24 '16 at 21:34
  • 1
    @vlaz I know I can't convert direct, for that I ask –  Sep 24 '16 at 21:36
  • Yes, the top hits from Google search are good and in active development. That said, if you're generating random numbers and not performing arithmetics on them, you can just generate several smaller numbers and concatenate them together as strings. – JJJ Sep 24 '16 at 21:41
  • 1
    @O_Z Thank you, I check that but have bugs in precision, for `999999999999999999999` return `"1000000000000000000000"`. –  Sep 24 '16 at 21:42
  • 1
    @JJJ not always top of Google is active development, is not axiomatic. I don't generate random numbers, I have in client (JavaScript) only a tileset, x:y, and when player move x++ or y++ or y-- or y--, the random numbers is in server side, only need send a big number of postion x,y to server. –  Sep 24 '16 at 21:46
  • I wasn't talking about a general case. In *this specific case* what you find on Google should work just fine. – JJJ Sep 24 '16 at 21:47
  • Possible duplicate of [Converting large numbers from binary to decimal and back in JavaScript](http://stackoverflow.com/questions/39334494/converting-large-numbers-from-binary-to-decimal-and-back-in-javascript) – guest271314 Sep 24 '16 at 21:52
  • 1
    @JJJ yes and not, library of 2,7KB for only do x++, is big, if exists alternative more light or without library is better, I won't use all library, only need a string where add or rest 1 each time, a scientific library maybe not enought fast for only x++ when also need render a map. –  Sep 24 '16 at 21:54
  • 1
    @guest271314 is same topic but different question and objective –  Sep 24 '16 at 21:55
  • How is objective different? – guest271314 Sep 24 '16 at 21:57
  • 1
    @guest271314 I don't want convert from binary to decimal and back, maybe for that? –  Sep 24 '16 at 22:07
  • @JHG Are you trying to send `"999999999999999999999"` to server? Or, receive `"999999999999999999999"` from server? Where is `"999999999999999999999"` created? Where is `1e+21` created? – guest271314 Sep 24 '16 at 22:15
  • 1
    @guest271314 server in Erlang receive X and Y position and return tile data, client in browser (JS), position is two vars (X,Y), when the player move map then X or Y do ++ or --, get tile data from server sending X and Y position, when the number is very big ++ make what when send X or Y to server the server receive 1e+21 and not the number 999999999999999999999. I need render the tileset in few ms, the server response is about 11-20ms, all page load and render map is about 400ms, I need do without a library big and slow because I need same speed or few more slow, more of 500-700ms is not good –  Sep 24 '16 at 22:27
  • You want to receive `999999999999999999999` from server, yes? – guest271314 Sep 24 '16 at 22:30
  • 1
    not "from", "in", I want receive the number in server, from browser, I have X in browser, user move, move, move, and X do X++, X++, X++, then I need the X position in server for in server return what tile image use in browser –  Sep 24 '16 at 22:37
  • 1
    I only need a fast code for do X++ but send a string with a long number to server, but need to be small and fast in browser, I don't need slow science calc for astronomy, I need only ++ fast for big number and convert to string for send to server, and need good performance –  Sep 24 '16 at 22:39
  • Variable `X` represents `999999999999999999999` that you want to send to server as string? – guest271314 Sep 24 '16 at 23:07
  • 1
    Yes, when player move rigth many times `X++` then `X` is `999999999999999999999` –  Sep 25 '16 at 12:35
  • Possible duplicate of [How to avoid scientific notation for large numbers in JavaScript?](https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript) –  Oct 03 '19 at 15:29

3 Answers3

1

you could use the BigNumber library (https://github.com/MikeMcl/bignumber.js/).

These 2 lines should do the trick:

BigNumber.config({EXPONENTIAL_AT: [-10, 30]});
var stringifiedLongNumber = new BigNumber('999999999999999999999').toString();

EXPONENTIAL_AT config parameter documentation: http://mikemcl.github.io/bignumber.js/#exponential-at

Benoit
  • 751
  • 5
  • 8
0

Maybe is better a small solution using optimizations of asm.js and work with strings for add and rest to the number, this a example simple but is possible do better:

var a = "999999";
var b = "";
var acarreo = 1|0;
console.log(a);
for(var i=a.length-1|0; i >= 0; i--){
    var j = parseInt(a[i])|0;
    var j = j + acarreo|0;
    if(j > 9){
        j-=10|0;
        acarreo=1|0;
    }else{
        acarreo=0|0;
    }
    b = j.toString() + b;
}
if(acarreo>0){
    a = acarreo.toString() + b;
}else{
    a = b;
}
console.log(a);
0

One possible solution for the problem in this case is not send new position to server, and only send a event of movement, then the server in Erlang can calculate new position with arbitrary long number (while have enought memory for a very big number), and not have the problem in JavaScript.