1.0.2 • Published 4 years ago

string-numbers v1.0.2

Weekly downloads
-
License
-
Repository
-
Last release
4 years ago

StringNumber

Exports a class defining a StringNumber. This class can be used to perform normal arithmetic operations. The operations, however, are often more accurate than normal operator based operations, this is because addition, subtraction and multiplication do not sacrifice any decimals, and the division operation allows an input overflow to ensure you get the precision you require. Be aware that these functions are slower than normal operations (especially division) in order to have increased precision.

const StringNumber = require("string-numbers");

Creating a StringNumber:

let stringNum = new StringNumber("1");
let stringNum = new StringNumber("1.2");
let stringNum = new StringNumber(1);
let stringNum = new StringNumber(1.2);

Class Properties:

Note: properties that accept a StringNumber will also accept a string or number (these types will be converted to a StringNumber).

Static Properties:

zero

StringNumber.zero(); // -> new StringNumber("0");

CleanNumberString Cleans an input string of all non number characters (including periods (.) and hyphens (-) as number characters).

StringNumber.CleanNumberString("-1a2b3c4d.5e6f7g8h"); // -> "-1234.5678"

DigitToInt Converts an input string digit (digits meaning only the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and digit, not plural, meaning only a single character is accepted) to an integer.

StringNumber.DigitToInt("5"); // -> 5

Non-static Properties:

toNumber Returns the StringNumber as a number.

let one = new StringNumber("1");
one.toNumber(); // -> 1

toString Returns the StringNumber as a string.

let one = new StringNumber("1");
one.toString(); // -> "1"

trim Mainly for internal use, removes any leading or trailing zeros with no affect to the value of the number.

let one = new StringNumber("0001.000");
one.trim().toString(); // -> "1"

equalTo Returns whether or not the input number is equal to the number stored in the StringNumber instance.

let one = new StringNumber("1")'
one.equalTo("1"); // -> true
one.equalTo("2"); // -> false

notEqualTo Returns whether or not the input number is NOT equal to the number stored in the StringNumber instance.

let one = new StringNumber("1");
one.notEqualTo("1"); // -> false
one.notEqualTo("2"); // -> true

greaterThan Returns whether the instance of StringNumber is greater than the input number.

let one = new StringNumber("1");
one.greaterThan("0"); // -> true
one.greaterThan("2"); // -> false

greaterThanOrEqualTo Returns whether the instance of StringNumber is greater than or equal to the input number.

let one = new StringNumber("1");
one.greaterThanOrEqualTo("0"); // -> true
one.greaterThanOrEqualTo("1"); // -> true
one.greaterThanOrEqualTo("2"); // -> false

lessThan` Returns whether the instance of StringNumber is less than the input number.

let one = new StringNumber("1");
one.lessThan("2"); // -> true
one.lessThan("0"); // -> false

lessThanOrEqualTo Returns whether the instance of StringNumber is less than or equal to the input number.

let one = new StringNumber("1");
one.lessThanOrEqualTo("2"); // -> true
one.lessThanOrEqualTo("1"); // -> true
one.lessThanOrEqualTo("0"); // -> false

round Rounds the instance of StringNumber to the nearest whole number.

let onePointFive = new StringNumber("1.5");
onePointFive.round().toString(); // -> "2"

floor Rounds the instance of StringNumber DOWN to the nearest whole number.

let onePointEight = new StringNumber("1.8");
onePointEight.floor().toString(); // -> "1"

floor Rounds the instance of StringNumber UP to the nearest whole number.

let onePointTwo = new StringNumber("1.2");
onePointTwo.ceil().toString(); // -> "2"

add Adds the input number to the instance of StringNumber.

let stringNum = new StringNumber("9.5");
stringNum.add("10.5").toString(); // -> "20"

subtract Subtracts the input number from the instance of StringNumber.

let stringNum = new StringNumber("15");
stringNum.subtract("3.5").toString(); // -> "11.5"

multiply Adds the input number to the instance of StringNumber.

let stringNum = new StringNumber("1.8");
stringNum.multiply("5.5").toString(); // -> "9.9"

divide Adds the input number to the instance of StringNumber. Divide is slightly different to the other operation methods, division can lead to an infinite number of decimal places and so divide also accepts an integer (overflow) which determines how many extra decimals to find should there be a remainder.

let stringNum = new StringNumber("264.264");
stringNum.divide("8").toString(); // -> "33.033"

let stringNum2 = new StringNumber("264.264");
stringNum2.divide("623", 10).toString(); // -> "0.4241797752808"

let stringNum3 = new StringNumber("264.264");
stringNum3.divide("623", 20).toString();// -> "0.42417977528089887640449"

modulus Returns the remainder when the instance of StringNumber is divided by the input number.

let stringNum = new StringNumber("5.1");
stringNum.modulus("2").toString(); // -> "1.1"

Chainable methods: trim, round, floor, ceil, add, subtract, multiply, divide and modulus.

A simple example: https://replit.com/@MattDESTROYER/String-Math?v=1

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago