ts config

This commit is contained in:
mol
2024-06-06 15:41:41 +08:00
parent 79cfa39e62
commit 80dd69b366
4 changed files with 9 additions and 0 deletions

6
src/index.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
// 模块的变量定义必须有 import 或者 export
export { }
declare global {
const uni: any;
}

27
src/js/floatAdd.ts Normal file
View File

@@ -0,0 +1,27 @@
import { isNumber } from "lodash-es";
/**
* 计算两个浮点数相加
* @param num1 数值1
* @param num2 数值2
* @returns 相加之和
* @description 使用两个数中小数点后位数较长的作为基数,抹平两个数的小数点,整数相加之后再除以基数,还原小数点。注意整体数值的位数不能超过最大整数范围
*/
export const floatAdd = (num1: number, num2: number) => {
if (!isNumber(num1) || !isNumber(num2)) return NaN;
let baseNum1: number;
let baseNum2: number;
try {
baseNum1 = num1.toString().split(".")[1].length;
} catch (e) {
baseNum1 = 0;
}
try {
baseNum2 = num2.toString().split(".")[1].length;
} catch (e) {
baseNum2 = 0;
}
const baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
const result = ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(2);
return +result;
};

8
src/uni-app/rpx2px.ts Normal file
View File

@@ -0,0 +1,8 @@
import { isNumber } from 'lodash-es';
export const rpx2px = (rpx) => {
if (!isNumber(rpx)) return 0;
const screenWidth = uni.getSystemInfoSync().screenWidth;
const result = (screenWidth * rpx) / 750
return result;
}