合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

28 lines
1005 B

// @ts-nocheck
/**
* 将字符串转换为数字
* @param val 要转换的字符串
* @returns 转换后的数字或原始字符串
*/
// #ifdef UNI-APP-X && APP
// function toNumber(val: string): number
// function toNumber(val: string): string
function toNumber(val: string): number|null {
const n = parseFloat(val); // 使用 parseFloat 函数将字符串转换为浮点数
return isNaN(n) ? null : n; // 使用 isNaN 函数判断是否为非数字,返回转换后的数字或原始字符串
}
export {toNumber}
// #endif
// #ifndef UNI-APP-X && APP
export function toNumber(val: string): number | string {
const n = parseFloat(val); // 使用 parseFloat 函数将字符串转换为浮点数
return isNaN(n) ? val : n; // 使用 isNaN 函数判断是否为非数字,返回转换后的数字或原始字符串
}
// #endif
// 示例
// console.log(toNumber("123")); // 输出: 123
// console.log(toNumber("3.14")); // 输出: 3.14
// console.log(toNumber("hello")); // 输出: "hello"