Buf*_*alo 14 javascript vue.js vuejs2
我找不到在VueJS中格式化数字的方法.我发现的只是用于格式化货币的内置货币过滤器和vue-numeric,需要进行一些修改才能看起来像标签.然后你不能用它来显示迭代的数组成员.
Buf*_*alo 23
安装numeral.js:
npm install numeral --save
Run Code Online (Sandbox Code Playgroud)
定义自定义过滤器:
<script>
var numeral = require("numeral");
Vue.filter("formatNumber", function (value) {
return numeral(value).format("0,0"); // displaying other groupings/separators is possible, look at the docs
});
export default
{
...
}
</script>
Run Code Online (Sandbox Code Playgroud)
用它:
<tr v-for="(item, key, index) in tableRows">
<td>{{item.integerValue | formatNumber}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
maf*_*tis 20
JavaScript 有一个内置函数。
如果您确定变量始终是数字而不是“数字字符串”,则可以执行以下操作:
{{ num.toLocaleString() }}
Run Code Online (Sandbox Code Playgroud)
如果您想安全,请执行以下操作:
{{ Number(num).toLocaleString() }}
Run Code Online (Sandbox Code Playgroud)
来源:https : //forum.vuejs.org/t/filter-numeric-with-comma/16002/2
Nas*_*lin 10
与浏览器的兼容性低,但Intl.NumberFormat()默认用法:
...
created: function() {
let number = 1234567;
console.log(new Intl.NumberFormat().format(number))
},
...
//console -> 1 234 567
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
Pez*_*vak 10
请注意,vue 3 中删除了过滤器,因此我们在全局属性中定义它:
app.config.globalProperties.$filters = {
formatNumber(number) {
return Intl.NumberFormat().format(number);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
<h3>{{ $filters.formatNumber(count) }}</h3>
Run Code Online (Sandbox Code Playgroud)
万一您真的想做一些简单的事情,以防万一:
<template>
<div> {{ commission | toUSD }} </div>
</template>
<script>
export default {
data () {
return {
commission: 123456
}
},
filters: {
toUSD (value) {
return `$${value.toLocaleString()}`
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果您想变得更复杂,请使用此代码或以下代码:
在 main.js
import {currency} from "@/currency";
Vue.filter('currency', currency)
Run Code Online (Sandbox Code Playgroud)
在 currency.js
const digitsRE = /(\d{3})(?=\d)/g
export function currency (value, currency, decimals) {
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : '$'
decimals = decimals != null ? decimals : 2
var stringified = Math.abs(value).toFixed(decimals)
var _int = decimals
? stringified.slice(0, -1 - decimals)
: stringified
var i = _int.length % 3
var head = i > 0
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
: ''
var _float = decimals
? stringified.slice(-1 - decimals)
: ''
var sign = value < 0 ? '-' : ''
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
_float
}
Run Code Online (Sandbox Code Playgroud)
并在template:
<div v-for="product in products">
{{product.price | currency}}
</div>
Run Code Online (Sandbox Code Playgroud)
您也可以在这里参考答案。
试试这个。如果您使用的是 vue.js 2
<template>
{{lowPrice | currency}}
</template>
<script>
data:(){
return {lowPrice: 200}
}
filters:{
currency(value) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD" }).format(value);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
vue.js 3 不再支持过滤器,因此您可以在计算中使用此逻辑
<template>
{{currency}}
</template>
<script>
data:(){
return {lowPrice: 200}
}
computed:{
currency() {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD" }).format(this.lowPrice);
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29627 次 |
| 最近记录: |