float_data_type
v1.1.1
Published
The library is used to create the `float` data type with its own set of methods
Downloads
4
Readme
Float
The library is used to create a float data type with its own set of methods.
The Float()
function can be called as a constructor with or without a parameter.
In both cases, it will return an instance of Float
with the value
property, where the value will be a fractional number.
In the latter case, the value of this property will be a random fractional number.
// 1
let float1 = new Float();
console.log(float1); // Float { value: random fractional number }
console.log(float1.value); // random fractional number
// 2
let float2 = new Float(-1.7);
console.log(float2); // Float { value: -1.7 }
Also Float()
can be called as a normal function and work similarly as if Float.random()
is called.
let float3 = Float();
console.log(float3); // random fractional number
The typeof
operator does not return the float
data type in this library.
console.log(typeof float1); // "object"
console.log(typeof float3); // "number"
Methods
Float.prototype.toFixed()
Works similarly to Number.prototype.toFixed()
.
let float4 = new Float(1.234);
console.log(float4); // Float { value: 1.234 }
console.log(
float4.toFixed(), // Float { value: "1" }
);
Float.random([min, max])
Generates a random fractional number.
let float5 = Float.random(20, 21);
console.log(float5); // some fractional number between 20 inclusive and 21 not inclusive.
Float.is()
Returns true
if the passed value is an instance of Float
or a fractional number primitive.
console.log(
Float.is(123), // false
Float.is("12.3"), // false
Float.is("1.5.5"), // false
Float.is(float1), // true
Float.is(float2), // true
Float.is(float3), // true
);
Float.like()
Returns true
in all cases of true
for Float.is()
, and also if the passed value is a string consisting entirely of a fractional number.
console.log(Float.like("12.3")); // true
How to install
- Install the library into your project using the command -
npm i float_data_type
. - The library uses CommonJS modules. Connect the module to your file as follows:
const { Float } = require('float_data_type');