Skip to content

余额格式化(useCurrencyFormat)

useCurrencyFormat 是一个用于金额格式化的 Vue 组合式函数,同时也提供其非响应式版本 formatCurrency

它能帮助你轻松处理:金额展示、小数位截断、千分位符号、货币符号等常见场景。

此 API 源码:GitHub

使用方式

vue
<template>
  {{ display }}
</template>

<script setup>
import { ref } from "vue";
import { useCurrencyFormat } from "see-u-ui";

const amount = ref(1234.567);

const display = useCurrencyFormat(amount, {
  decimals: 2,
  symbol: "¥",
  useGrouping: true,
});
</script>
vue
<script setup>
import { formatCurrency } from "see-u-ui";

formatCurrency(1234.567, {
  decimals: 2,
  symbol: "¥",
});
</script>

格式化规则

功能说明
截断小数非四舍五入,例如 12.349 → 12.34
自动补零指定 decimals=2 时,会补齐两位小数
千分位例如:1,234,567.89
符号前缀支持 ¥$ 或任意自定义字符串
占位符处理当值为 null / undefined / '' 显示 -

API

useCurrencyFormat(amount, options?)

响应式金额格式化

参数类型默认值说明
amountMaybeRefOrGetter<number | string>输入金额(支持响应式)
optionsCurrencyOptions{}配置项
options.decimalsnumber2保留小数位(截断)
options.symbolstring''金额符号
options.placeholderstring'-'无效金额时显示内容
options.useGroupingbooleantrue是否启用千分位

返回: ComputedRef<string>


formatCurrency(value, options?)

非响应式金额格式化工具函数

参数类型默认值说明
valuenumber | string输入金额
optionsCurrencyOptions{}格式化配置

返回: string

辽 ICP 备 2025070134 号