index.d.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
  3. *
  4. * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
  5. */
  6. declare type NumberType = number | string;
  7. /**
  8. * Correct the given number to specifying significant digits.
  9. *
  10. * @param num The input number
  11. * @param precision An integer specifying the number of significant digits
  12. *
  13. * @example strip(0.09999999999999998) === 0.1 // true
  14. */
  15. declare function strip(num: NumberType, precision?: number): number;
  16. /**
  17. * Return digits length of a number.
  18. *
  19. * @param num The input number
  20. */
  21. declare function digitLength(num: NumberType): number;
  22. /**
  23. * Convert the given number to integer, support scientific notation.
  24. * The number will be scale up if it is decimal.
  25. *
  26. * @param num The input number
  27. */
  28. declare function float2Fixed(num: NumberType): number;
  29. /**
  30. * Accurate multiplication.
  31. *
  32. * @param nums The numbers to multiply
  33. */
  34. declare const times: (...nums: NumberType[]) => number;
  35. /**
  36. * Accurate addition.
  37. *
  38. * @param nums The numbers to add
  39. */
  40. declare const plus: (...nums: NumberType[]) => number;
  41. /**
  42. * Accurate subtraction.
  43. *
  44. * @param nums The numbers to subtract
  45. */
  46. declare const minus: (...nums: NumberType[]) => number;
  47. /**
  48. * Accurate division.
  49. *
  50. * @param nums The numbers to divide
  51. */
  52. declare const divide: (...nums: NumberType[]) => number;
  53. /**
  54. * Accurate rounding method.
  55. *
  56. * @param num The number to round
  57. * @param decimal An integer specifying the decimal digits
  58. */
  59. declare function round(num: NumberType, decimal: number): number;
  60. /**
  61. * Whether to check the bounds of number, default is enabled.
  62. *
  63. * @param flag The value to indicate whether is enabled
  64. */
  65. declare function enableBoundaryChecking(flag?: boolean): void;
  66. export { strip, plus, minus, times, divide, round, digitLength, float2Fixed, enableBoundaryChecking };
  67. declare const _default: {
  68. strip: typeof strip;
  69. plus: (...nums: NumberType[]) => number;
  70. minus: (...nums: NumberType[]) => number;
  71. times: (...nums: NumberType[]) => number;
  72. divide: (...nums: NumberType[]) => number;
  73. round: typeof round;
  74. digitLength: typeof digitLength;
  75. float2Fixed: typeof float2Fixed;
  76. enableBoundaryChecking: typeof enableBoundaryChecking;
  77. };
  78. export default _default;