cxc-szcx-uniapp/uni_modules/lime-shared/toNumber/index.ts

28 lines
1017 B
TypeScript
Raw Normal View History

2024-09-14 02:26:50 +00:00
// @ts-nocheck
/**
*
* @param val
* @returns
*/
// #ifdef APP-IOS || APP-ANDROID
// 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 APP-IOS || APP-ANDROID
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"