123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const mulPrecision = 12, divPrecision = 12;
- function digitLength (num) {
- // 兼容科学计数
- var eSplit = num.toString().split(/[eE]/);
- var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
- return len > 0 ? len : 0;
- }
- function powLength (num) {
- var rs = num.toString();
- if (rs.indexOf('+') > 0) {
- return rs.match(/0*$/g).length();
- } else {
- const eSplit = rs.split(/[eE]/);
- const len = Number(eSplit[1]) - this.digitLength(eSplit[0]);
- return len > 0 ? len : 0;
- }
- }
- function round (num, digit) {
- return Math.round(num * Math.pow(10, digit)) / Math.pow(10, digit);
- }
- function add(num1, num2) {
- var d1 = this.digitLength(num1), d2 = this.digitLength(num2);
- return this.round(num1 + num2, Math.max(d1, d2));
- }
- function sub(num1, num2) {
- var d1 = this.digitLength(num1), d2 = this.digitLength(num2);
- return this.round(num1 - num2, Math.max(d1, d2));
- }
- function mul(num1, num2) {
- return this.round(num1 * num2, mulPrecision);
- }
- function div(num1, num2) {
- return this.round(num1 / num2, divPrecision);
- }
- module.exports = {
- digitLength: digitLength,
- powLength: powLength,
- round: round,
- add: add,
- sub: sub,
- mul: mul,
- div: div,
- };
|