This question already has answers here:
Generate big numbers with Math random in Javascript
(4 answers)
Closed 1 year ago.
I'm using this one liner to generate a random 18 digit number :
Math.floor(100000000000000000 + Math.random() * 900000000000000000)
The problem is that it always contains one or more zeros at the end, and I don't understand why.
How can I generate a really random number that always contains 18 digits, and without a leading zero?
You have too many digits, you're running against the limit to precision in a javascript number.
It looks like a Problem with the max number Range. You can hack it like this. And it is a better Random number.
Math.floor(10000000000000000 + Math.random() * 90000000000000000)+ "" + Math.floor(Math.random()* 100)
But if you parse to int you have the same problem.
The solution for this so far is to generate two numbers (10 digits + 8 digits) & concatenate them as follow:
Math.floor(1000000000 + Math.random() * 9000000000) + "" + Math.floor(10000000 + Math.random() * 90000000)
Related
This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 3 years ago.
I would like to get a numeric value in a string which is in the format 1 111.
I used a regex to extract it:
([0-9]*)\s([0-9]*)
then I thought that I will obtain the correct result with this operation:
regex_result[1]*1000+regex_result[2]
But actually I just have to addition them and I do not understand why.
var str= "Bit rate : 5 333 kb/s"
var bitrate= str.match(/Bit\srate\s*:\s([0-9]*)\s([0-9]*)\s/);
console.log(bitrate);
//attempted result, but incorrect code
console.log(bitrate[1]+bitrate[2]);
//attempted correct code, but wrong result
console.log(bitrate[1]*1000+bitrate[2]);
Here, the second captured group just so happens to be 3 characters long, so multiplying the first captured group by 1000 and adding it to the second group will just so happen to produce the same result as plain concatenation.
But you have to add them together properly first. Your second attempt isn't working properly because the right-hand side of the + is a string, and a number + a string results in concatenation, not addition:
var str = "Bit rate : 5 333 kb/s"
var bitrate = str.match(/Bit\srate\s*:\s([0-9]*)\s([0-9]*)\s/);
console.log(bitrate[1] * 1000 + Number(bitrate[2]));
If the input isn't guaranteed to have exactly 3 digits in the second capturing group, the concatenation method won't work.
You can parse them as ints instead of manipulating strings
var str= "Bit rate : 5 333 kb/s"
var bitrate= str.match(/Bit\srate\s*:\s([0-9]*)\s([0-9]*)\s/);
console.log(bitrate);
console.log(parseInt(bitrate[1] * 1000) + parseInt(bitrate[2]));
This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 5 years ago.
I'm given numbers in cents:
eg.
102042
982123
121212
I want to convert them to dollars. I figured the easiest way to do this is to convert the digits into a string and add a decimal.
eg.
1020.42
9821.23
1212.12
I'm currently doing this but it only rounds to 1 decimal. What would I need to do it make it round to 2 decimals?
var number = 102042
number.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
UPDATE: I found out what my issue was. It wasn't the rounding but the fact that I combined a lodash filter to a division incorrectly. Thanks again!
A cent is 1/100th of a dollar, so you can calculate the dollar amount by doing
dollars = number / 100
var number = 102042;
console.log(number/100);
The easiest way is to do it with the number itself, you can multiply by 0.01 or divide by 100 and you'll get that amount in dollars:
var numbers = [102042,982123,121212];
for(num of numbers)
console.log(num/100);
This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 7 years ago.
I am trying to generate a random number with a minimum and maximum value. Currently I have the following variable that generates numbers between 0-2000. I want to be able to generate a between 500 and 2000. Will need to create local variables to achieve this?
random = Math.floor(Math.random()*2000);
Math.random() * (max - min) + min);
If you need to generate more than one random number you need more than one variable.
Edit: One parentheses was missing in the code example
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);
This question already has answers here:
Convert a decimal number to a fraction / rational number
(12 answers)
Closed 9 years ago.
I want to convert the decimal number 5.5 to 5 1/2. How can i do that?
I need to check for the number. If it is 5.5 then convert it to 5 1/2.
Please advice.
Using the fraction library and the vanilla framework instead of jQuery, i suppose something like this would work:
https://github.com/ekg/fraction.js
var num = 5.5
, rounded = Math.floor(num)
, rest = num - Math.floor(num);
var f = new Fraction(1, rest);
console.log(rounded + ' ' + f.numerator + '/' + f.denominator);
example: http://jsfiddle.net/QwTPY/
Try https://github.com/ekg/fraction.js or maths.js. Fraction.js was built for the purpose of working with fractions.