外观
toNumber 数字转换
约 164 字小于 1 分钟
2024-01-01
介绍
将字符串或其他类型的值转换为数字类型。
toNumber(value: string | number): number
- 参数
- value: 要转换的值,可以是字符串或数字
- 返回值
<Number>转换后的数字,如果输入为空字符串则返回 0
示例代码
<template>
<view>
<input v-model="inputValue" type="text" placeholder="请输入数字" />
<button @click="convertToNumber">转换为数字</button>
<view v-if="result !== null">
<text>转换结果: {{ result }}</text>
<text>类型: {{ typeof result }}</text>
</view>
</view>
</template>
<script setup lang="uts">
import { ref } from 'vue'
import { toNumber } from '@/uni_modules/uview-unix'
const inputValue = ref('')
const result = ref<number | null>(null)
const convertToNumber = () => {
result.value = toNumber(inputValue.value)
console.log('转换结果:', result.value, typeof result.value)
}
</script>