Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

When evaluating values in JavaScript, it’s easy to run into 0.1+0.2! The =0.3 problem is caused by a loss of precision during the conversion of a value from decimal to binary. This article recommends the decimal.js library to solve such problems.

Official API documentation: address.

Installation and introduction

Execute the command NPM I decimal.js –save to install.

Import {Decimal} from ‘decimal.js’ is introduced into use.

Dealing with numerical

Before using decimal.js to evaluate a value, you process the value in Decimal, which is a constructor that returns a Decimal object.

const decimal_10 = new Decimal(10);
console.log(decimal_10)
Copy the code

The argument to the Decimal constructor can be either a string or a number, and the string can only be a string representing a number, such as ’10’, or it can be passed NaN or Infinity.

Arguments prefixed with 0x or 0x represent hexadecimal, 0b or 0b represent binary, and /0o or 0o represent octal. Of course, these are not commonly used, just know.

Make sure that the parameter cannot be null, undefined, or null. Otherwise, an error will be reported.

Formatting value

After the value is processed, it cannot be displayed directly on the page. It has to be formatted. Commonly used by the following types.

  • ToString () is formatted as a string.

  • ValueOf () is formatted as a string, but with the zero sign console.log((new Decimal(-0)).valueof ()) // -0.

  • ToNumber () is formatted to type Number and converted to the value of the original Number.

  • ToFixed () is formatted as a string and used the same way as toFixed() in JS, except that toFixed() in decimal.js takes a second argument that sets the type to round.

And something you don’t usually use

  • ToBinary () formats toBinary.

  • ToHexadecimal () is formatted in hexadecimal.

  • ToOctal () is formatted as octal.

subtracting

The most common use in computation is addition, subtraction, multiplication, and division. Take a look at the addition, subtraction, multiplication, and division in Decimal. js, which can be used either as static methods of a Decimal class or as instance methods of a Decimal class.

  • add
Decimal.add(1, 2); // 3 const x = new Decimal(1); const result = x.plus(2); / / 3Copy the code
  • subtraction
Decimal.sub(3, 1); // 2 const x = new Decimal(3); const result = x.sub(1); / / 2Copy the code
  • The multiplication
Decimal.mul(3, 2); // 6 const x = new Decimal(3); const result = x.mul(2); / / 6Copy the code
  • division
Decimal.div(6, 2); // 3 const x = new Decimal(6); const result = x.div(2); / / 3Copy the code

The divisor (denominator) cannot be 0 when you use division.

Take the absolute value

Decimal.abs(-3); // 3 const x = new Decimal(-3); const result = x.abs(); / / 3Copy the code

To compare

  • Is greater than
const x = new Decimal(2);
const y = new Decimal(1);
console.log(x.greaterThan(y)); // true
Copy the code
  • Greater than or equal to
const x = new Decimal(2);
const y = new Decimal(2);
console.log(x.greaterThanOrEqualTo(y)); // true
Copy the code
  • Less than
const x = new Decimal(2);
const y = new Decimal(1);
console.log(y.lessThan(x)); // true
Copy the code
  • Less than or equal to
const x = new Decimal(2);
const y = new Decimal(2);
console.log(y.lessThanOrEqualTo(x)); // true
Copy the code
  • Is equal to the
const x = new Decimal(2);
const y = new Decimal(2);
console.log(x.equals(y)); // true
Copy the code

judge

  • Is it an integer?
const x = new Decimal(1) 
x.isInteger(); // true 
const y = new Decimal(123.456) 
y.isInt(); // false
Copy the code
  • Is it positive
x = new Decimal(0); x.isPositive(); // true y = new Decimal(-2); y.isPos(); // falseCopy the code
  • Is it negative
x = new Decimal(-0); 
x.isNegative(); // true 
y = new Decimal(2); 
y.isNeg(); // false
Copy the code