0

I need to generate a 12 digit and 13 decimals float value like this:

123438767411.238792938

How can I do that in Javascript/Jquery? Is it possible to generate this code using JavaScript?

I was trying like this:

v1 = Math.floor((Math.random()*10000000000)+1);
v2 = Math.floor((Math.random()*100000000)+1);
v = v1.toString() + "." + v2.toString();

But this is not working!

Laxmikant Ratnaparkhi
  • 4,745
  • 5
  • 33
  • 49

1 Answers1

1

(assuming you mean in the form of a string, not as a number, because IEEE 754 can't have that many significant digits)

must the integer part be 12 digits or can it be 1 or 123? If it can be 12 digits or shorter, then it can be

(Math.floor (Math.random() * Math.pow(10,12)) 
    + (Math.floor (Math.random() * Math.pow(10,13)) 
      / Math.pow(10,13)).toString().substring(1))

note that the above could have an issue when the decimal part turns out to be 0, although the chance is really small. (then the .0 part is gone, although we can use a conditional to add it when it is so). Or we can treat the decimal part 123 not as .0000000000123 but as .123 and use:

(Math.floor (Math.random() * Math.pow(10,12)) 
    + "." + Math.floor (Math.random() * Math.pow(10,13)))

But it depends whether we care about 123 becoming .123 and 1230 also becoming .1230 because if we do care about it, we can say .123 is the same as .1230.

Also, if we want to have the form such as 000042987017.0790946977900 as well, so that it is always 12 digit integer and 13 digit decimal, then either we can do zero padding or use something like this:

sample: http://jsfiddle.net/jL4t4/1/

var i, s = "";

for (i = 0; i < 26; i++) {
    s += (i === 12) ? "." : Math.floor(Math.random() * 10);
}
nonopolarity
  • 146,324
  • 131
  • 460
  • 740