大家好,我正在嘗試格式化 JSON 給我帶來的價值,我看到了一些建議,但我仍然無法轉換價值。這就是我的代碼結構
<template>
...
<div class="currency-selection">
<input type="text" :value="conversionValue * cryptoQuantity " readonly />
...
<template>
<script>
export default {
name: 'CurrencySelect',
data: () => ({
conversionValue: 0,
cryptoQuantity: 1
}),
axios
.get(
"https://min-api.cryptocompare.com/data/price?fsym=btc&tsyms=COP"
)
.then((res) => {
this.conversionValue = res.data.COP;
})
.catch((e) => {
console.log(e);
});
}
現在該值為 169057977.17 但我希望它顯示如下:169.057.977,17
uj5u.com熱心網友回復:
您可以使用toLocaleString根據您定義的語言和地區來格式化數字。
使用Number.toLocaleString('es-CO')得到這個:169.057.977,17
有關受支持的語言環境串列,請參閱此內容
看這個例子
<script>
export default {
data() {
return {
conversionValue: 0,
cryptoQuantity: 1
}
},
async mounted () {
this.conversionValue = await fetch("https://min-api.cryptocompare.com/data/price?fsym=btc&tsyms=COP").then(raw => raw.json()).then(json => json.COP)
}
}
</script>
<template>
<input type="text" :value="(this.cryptoQuantity * this.conversionValue).toLocaleString('es-CO')"/>
</template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454957.html
上一篇:重繪部分組件的問題
