get-random-float
Version:
Gets random float
289 lines (178 loc) • 6.1 kB
Markdown
Using NPM:
```sh
npm i get-random-float
```
```js
const Float = require('get-random-float');
```
The `Float()` constructor creates `Float` objects.
```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
```
```js
let float = new Float(-1.7);
float; // Float { value: -1.7 }
float.value; // -1.7
typeof float; // object
```
`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.
```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"
```
```js
let float = Float(1.7);
float; // 1.7
typeof float; // "number"
```
The `Float.random()` static method returns a pseudo-random float from a range, that can be specified in arguments or otherwise chosen randomly.
```js
Float.random([min, max])
```
- `min` optional
The return value will be bigger than `min`.
- `max` optional
The return value will be smaller than `max`.
A pseudo-random float between `min` (exclusive) and `max` (exclusive).
```js
let float = Float.random(20, 21);
float; // some random float between 20 and 21 not inclusive
```
The `Float.is()` static method determines whether the passed argument is either a `Float` instance or a float primitive number.
```js
Float.is(value)
```
- `value`
The value to be tested for being either a `Float` instance or a float primitive number.
The boolean value `true` if the given value is either a `Float` instance or a float primitive number. Otherwise `false`.
```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
```
```js
const float = new Float();
float; // Float { value: random float }
Float.is(float), // true
```
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.
```js
Float.like(value)
```
- `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.
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`.
```js
Float.like("12.3"), // true
Float.like(12.3), // true
```
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.
```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.
The given `Float` instance with the float number in its `value` property formatted using fixed-point notation.
```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}
```
The `valueOf()` method of `Float` instances converts `Float` intances to a number primitive.
```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
```
```js
float + "kg"; // "1.5kg"
float + 1; // 2.5
```
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.
```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
```