Number Generation with a Decimal

Hi,

Could anyone advise me on how to get a random number between 1 - 50 with an additional 2 decimal numbers between 1 - 99?

e.g. so i could get 1.54 or 23.91

I can find examples of whole numbers, but nothing for items with decimals.

I’m sure there is a nicer way than:

const pounds = (“random_number”, _.random(1, 50));

const pence = (“random_number”, _.random(10, 99));

const paymentAmount = pounds + “.” + pence

Any help is appreciated, thank you

This should return a floating-point number but I’m not sure if that’s fixed to 2 decimal places.

const pounds = _.random(1, 50, true)

https://lodash.com/docs/4.17.15#random

You could fix it to 2 places by using toFixed(2):

const pounds =  _.random(1, 50, true)

console.log(pounds.toFixed(2))
1 Like