UNPKG

get-random-float

Version:

Gets random float

289 lines (178 loc) 6.1 kB
# Get random float ## Installation Using NPM: ```sh npm i get-random-float ``` ## Usage ```js const Float = require('get-random-float'); ``` ## `Float()` constructor The `Float()` constructor creates `Float` objects. ### Syntax ```js new Float([value]) ``` #### Parameters - `value` optional The numeric value of the object being created. If this argument is omitted, a random float is chosen instead. ### Examples #### #1 Creating `Float` objects with no arguments ```js const float = new Float(); float; // Float { value: random float } float.value; // random float ``` #### #2 Creating `Float` objects with an argument ```js let float = new Float(-1.7); float; // Float { value: -1.7 } float.value; // -1.7 typeof float; // object ``` ## `Float()` `Float()` can be called as a function. It returns a random float when used with no arguments. Otherwise it returns the given value converted to a number. ### Syntax ```js Float([value]) ``` #### Parameters - `value` optional The value to be converted to a number. If the agrument can't be converted, the function returns `NaN`. If this argument is omitted, a random float is chosen instead. #### Return value When `Float()` is called as a function, it returns `value` converted to a number primitive. If value is absent, a random float is chosen instead. ### Examples #### #1 Using `Float()` as a function with no arguments ```js const float = Float(); float; // random float typeof float; // "number" ``` #### #2 Using `Float()` as a function with an argument ```js let float = Float(1.7); float; // 1.7 typeof float; // "number" ``` ## `Float.random()` The `Float.random()` static method returns a pseudo-random float from a range, that can be specified in arguments or otherwise chosen randomly. ### Syntax ```js Float.random([min, max]) ``` #### Parameters - `min` optional The return value will be bigger than `min`. - `max` optional The return value will be smaller than `max`. #### Return value A pseudo-random float between `min` (exclusive) and `max` (exclusive). ### Examples #### #1 Getting a random float between two values ```js let float = Float.random(20, 21); float; // some random float between 20 and 21 not inclusive ``` ## `Float.is()` The `Float.is()` static method determines whether the passed argument is either a `Float` instance or a float primitive number. ### Syntax ```js Float.is(value) ``` #### Parameters - `value` The value to be tested for being either a `Float` instance or a float primitive number. #### Return value The boolean value `true` if the given value is either a `Float` instance or a float primitive number. Otherwise `false`. ### Examples #### #1 Using `Float.is()` ```js Float.is(1.5); // true Float.is(123); // false Float.is("12.3"); // false Float.is("js"); // false Float.is("1.5.5"); // false ``` #### #2 Using `Float.is()` ```js const float = new Float(); float; // Float { value: random float } Float.is(float), // true ``` ## `Float.like()` The `Float.like()` static method determines whether the passed argument is either a `Float` instance or a float primitive number or a string containing only a float number. ### Syntax ```js Float.like(value) ``` #### Parameters - `value` The value to be tested for being either a `Float` instance or a float primitive number or a string containing only a float number. #### Return value The boolean value `true` if the given value is either a `Float` instance or a float primitive number or a string containing only a float number. Otherwise `false`. ### Examples #### #1 Using Float.like() ```js Float.like("12.3"), // true Float.like(12.3), // true ``` ## `Float.prototype.toFixed()` The `toFixed()` method of `Float` instances works like `Number.prototype.toFixed()`. Formats the float number in the `value` property of the given `Float` instance using fixed-point notation. ### Syntax ```js toFixed([digits]) ``` #### Parameters - `digits` optional The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive. If this argument is omitted, it is treated as 0. #### Return value The given `Float` instance with the float number in its `value` property formatted using fixed-point notation. ### Examples #### #1 Using `toFixed()` ```js const float = new Float(); float.toFixed(2); // Float { value: random float with two digits after the decimal point} float.toFixed(); // Float { value: random float with zero digits after the decimal point} ``` ## `Float.prototype.valueOf()` The `valueOf()` method of `Float` instances converts `Float` intances to a number primitive. ### Syntax ```js valueOf() ``` #### Parameters None. #### Return value The value of the `value` property of the given `Float` instance as a number primitive. ### Examples #### #1 Converting a `Float` instance to a number primitive ```js let float = new Float(1.5); float; // Float { value: 1.5 } float.valueOf(); // 1.5 ``` #### #2 `valueOf()` affecting implicit conversion ```js float + "kg"; // "1.5kg" float + 1; // 2.5 ``` ## `Float.prototype.equals()` The `equals()` method of `Float` instances compares the value of the `value` property of the `Float` instance to the given argument using the strict equality operator. ### Syntax ```js equals(value) ``` #### Parameters - value The number primitive to be compared to the value of the `value` property of the `Float` intance. #### Return value The boolean value which is the result of using the strict comparison operator to compare the value of the `value` property of the `Float` intance to the given argument. ### Examples #### #1 Comparing a float to a `Float` instance ```js let float = new Float(2.5); float; // Float { value: 1.5 } float.equals(2.5); // true ```