外观
numberToChinese 数字转中文大写
约 191 字小于 1 分钟
2024-01-01
介绍
将数字转换为中文大写形式,适用于金额等场景。
numberToChinese(amount: number): string
- 参数
- amount: 要转换的数字
- 返回值
<String>中文大写形式的数字字符串
示例代码
<template>
<view>
<text>数字转中文大写</text>
<input v-model="inputAmount" type="text" placeholder="请输入数字" />
<button @click="convertToChinese">转换</button>
<view v-if="chineseAmount">
<text>中文大写: {{ chineseAmount }}</text>
</view>
</view>
</template>
<script setup lang="uts">
import { ref } from 'vue'
import { numberToChinese } from '@/uni_modules/uview-unix'
const inputAmount = ref('')
const chineseAmount = ref('')
const convertToChinese = () => {
const amount = parseFloat(inputAmount.value)
chineseAmount.value = numberToChinese(amount)
console.log('中文大写:', chineseAmount.value)
}
// 示例结果
console.log(numberToChinese(1234.56)) // 壹仟贰佰叁拾肆元伍角陆分
console.log(numberToChinese(0)) // 零元整
console.log(numberToChinese(-123)) // 欠壹佰贰拾叁元整
</script>