合同小程序前端代码仓库
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.

27 lines
1005 B

  1. // @ts-nocheck
  2. /**
  3. *
  4. * @param val
  5. * @returns
  6. */
  7. // #ifdef UNI-APP-X && APP
  8. // function toNumber(val: string): number
  9. // function toNumber(val: string): string
  10. function toNumber(val: string): number|null {
  11. const n = parseFloat(val); // 使用 parseFloat 函数将字符串转换为浮点数
  12. return isNaN(n) ? null : n; // 使用 isNaN 函数判断是否为非数字,返回转换后的数字或原始字符串
  13. }
  14. export {toNumber}
  15. // #endif
  16. // #ifndef UNI-APP-X && APP
  17. export function toNumber(val: string): number | string {
  18. const n = parseFloat(val); // 使用 parseFloat 函数将字符串转换为浮点数
  19. return isNaN(n) ? val : n; // 使用 isNaN 函数判断是否为非数字,返回转换后的数字或原始字符串
  20. }
  21. // #endif
  22. // 示例
  23. // console.log(toNumber("123")); // 输出: 123
  24. // console.log(toNumber("3.14")); // 输出: 3.14
  25. // console.log(toNumber("hello")); // 输出: "hello"