[英]toFixed() a large number in a template VueJS
I display a reward which is a number with 9 decimals (must stay like this for the calculation)我显示一个奖励,它是一个带 9 位小数的数字(计算时必须保持这样)
But I would like to display only 4 decimal但我只想显示小数点后 4 位
My using a template in VueJS我在 VueJS 中使用模板
<template>
<div class="mb-2">Reward: {{ myAcccount.reward.accrued.toFixed(4) * (10 ** -9) - appAcccount.reward.paid.toFixed(4) * (10 ** -9)}}</div>
</template>
But I cant applied toFixed() like this, do you know a way to do that directly in the template?但是我不能像这样应用 toFixed() ,你知道直接在模板中这样做的方法吗?
Currently it diplay:目前显示:
Reward: 0.0022680540000000003
奖励:0.0022680540000000003
So I would like所以我想
Reward: 0.0022
奖励:0.0022
toFixed()
is returning a string and not a number. toFixed()
返回一个字符串而不是一个数字。 For better readability I would extract the logic in an own function aswell为了更好的可读性,我也会在自己的 function 中提取逻辑
EDIT: added Math instead of toFixed
you could write a function which transform your number into a decimal.编辑:添加 Math 而不是
toFixed
你可以写一个 function 将你的数字转换成小数。
const transformNumber = (number) => {
return Math.trunc(number * 10000)/10000;
}
and like mentioned a computed property:就像提到的一个计算属性:
computed: {
reward(): {
return transformNumber(myAcccount.reward.accrued) * (10 ** -9) - transformNumber(appAcccount.reward.paid) * (10 ** -9)
}
}
You can try applying.toFixed() to the end of the calculation by enclosing the math part inside ().您可以通过将数学部分包含在 () 中来尝试将 .toFixed() 应用到计算的末尾。
<div class="mb-2">Reward: {{ (myAcccount.reward.accrued * (10 ** -9) - appAcccount.reward.paid * (10 ** -9)).toFixed(4)}}</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.