22 * @Author : liwb lwbhtml@163.com
33 * @Date : 2025-04-18 13:54:44
44 * @LastEditors : liwb lwbhtml@163.com
5- * @LastEditTime : 2025-04-18 14:01:21
5+ * @LastEditTime : 2025-04-18 15:23:27
66 * @FilePath : /cloud-utils/src/math.ts
77 * @Description : 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
88 */
@@ -27,7 +27,7 @@ import { Decimal } from 'decimal.js';
2727 * @returns {number } 商
2828 * @throws {Error } 当除数为0时抛出错误
2929 */
30- function mathDivide ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
30+ export function mathDivide ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
3131 if ( arg2 === 0 || arg2 === '0' ) {
3232 throw new Error ( '除数不能为0' ) ;
3333 }
@@ -48,7 +48,7 @@ function mathDivide(arg1: number | string, arg2: number | string, precision: num
4848 * @param {number } [precision] 精度,默认保留10位小数
4949 * @returns {number } 和
5050 */
51- function mathAdd ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
51+ export function mathAdd ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
5252 const x = new Decimal ( arg1 ) ;
5353 const y = new Decimal ( arg2 ) ;
5454 return Number ( x . plus ( y ) . toDecimalPlaces ( precision ) ) ;
@@ -61,7 +61,7 @@ function mathAdd(arg1: number | string, arg2: number | string, precision: number
6161 * @param {number } [precision] 精度,默认保留10位小数
6262 * @returns {number } 差
6363 */
64- function mathSubtract ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
64+ export function mathSubtract ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
6565 const x = new Decimal ( arg1 ) ;
6666 const y = new Decimal ( arg2 ) ;
6767 return Number ( x . minus ( y ) . toDecimalPlaces ( precision ) ) ;
@@ -74,19 +74,12 @@ function mathSubtract(arg1: number | string, arg2: number | string, precision: n
7474 * @param {number } [precision] 精度,默认保留10位小数
7575 * @returns {number } 积
7676 */
77- function mathMultiply ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
77+ export function mathMultiply ( arg1 : number | string , arg2 : number | string , precision : number = 10 ) : number {
7878 const x = new Decimal ( arg1 ) ;
7979 const y = new Decimal ( arg2 ) ;
8080 return Number ( x . times ( y ) . toDecimalPlaces ( precision ) ) ;
8181}
8282
83- export {
84- mathDivide ,
85- mathAdd ,
86- mathSubtract ,
87- mathMultiply
88- } ;
89-
9083// 测试用例
9184// console.log(mathDivide(5.1, 3)); // 1.7
9285// console.log(mathDivide('5.1', '3')); // 1.7
0 commit comments