Search code examples
javascriptphotoshop-script

Javascript extract last numbers from Math.random


So I'm trying to do a script for Photoshop with javascript and I can't get the last 6 number from a Math.random.

I tried using the same code as in Strings with "randomID.substr(randomID.length - 6);" or "randomID.substr(-6);" but that didn't work.

var kodi = 'FJ0B';
var randomID = Math.floor(Math.random() * (999999999999 - 100000000000 + 1) + 100000000000);
var lastSix = randomID.toFixed(-6);

var kontrataLayer = (kodi.charAt(0) + lastSix);

Math.floor works fine, I need it with 12 digits for another function. Thank you.


Solution

  • What about:

    var randomIDString = randomID.toString();
    
    var lastSix = Number(randomIDString.substr(randomIDString.length - 6));
    

    For substr to work you need to convert the number to a string. Maybe that's why it didn't work for you earlier?