1.3.2 • Published 12 months ago

get-random-float v1.3.2

Weekly downloads
-
License
-
Repository
-
Last release
12 months ago

Get random float

Installation

Using NPM:

npm i get-random-float

Usage

const Float = require('get-random-float');

Float() constructor

The Float() constructor creates Float objects.

Syntax

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

const float = new Float();
float; // Float { value: random float }
float.value; // random float

#2 Creating Float objects with an argument

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

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

const float = Float();
float; // random float
typeof float; // "number"

#2 Using Float() as a function with an argument

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

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

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

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()

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()

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

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()

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

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()

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

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

let float = new Float(1.5);
float; // Float { value: 1.5 }

float.valueOf(); // 1.5

#2 valueOf() affecting implicit conversion

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

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

let float = new Float(2.5);
float; // Float { value: 1.5 }

float.equals(2.5); // true
1.3.2

12 months ago

1.3.1

1 year ago

1.2.3

1 year ago

1.3.0

1 year ago

1.2.2

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.2.1

1 year ago

1.0.0

1 year ago