How to use the Decimal type of Js
This article focuses on "how to use Js's Decimal type". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Decimal type of Js.
Decimal is a data type of SQLServer, MySql and other databases, does not belong to the floating point type, and can be defined as the integer part and the decimal part of the digits. The use of exact decimal types can not only ensure more accurate data calculation, but also save storage space.
Decimal import file
About npm instruction reference
/ * * Node.js*/$npminstall--savedecimal.jsvarDecimal=require ('decimal.js'); / * * ES6 module mode * / import {Decimal} when from'decimal.js';// was first joined, IDEA may not have responded, so it will be grayed out. It will be fine after a while.
The use of Decimal
/ / addition operation vara=0.13;varb=0.25;console.log ('addition operation aplanetta (newDecimal (b); console.log (' using Decimaljsa+b=',newDecimal (a) .add (newDecimal (b)). ToNumber ()); / / subtraction vara=1.0;varb=0.99console.log ('direct subtraction operation a subtraction operation, aantha bauhinia); console.log (' using Decimaljsa-b=',newDecimal (a) .sub (newDecimal (b)). ToNumber (). ToFixed (2) / / preserve two-digit data / / multiply vara=1.01;varb=1.02;console.log ('direct multiplication adirectmultiplication adirectmultiplication); console.log (' use Decimaljsa*b=',newDecimal (a) .mul (newDecimal (b)). ToNumber ()); / / division vara=0.033;varb=10;console.log ('direct division operation a toNumber (b)); console.log (' use Decimaljsa/b=',newDecimal (a) .div (newDecimal (b)). ToNumber ())
At this point, I believe you have a deeper understanding of "how to use the Decimal type of Js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!