如何在 VueJS 中格式化数字

新手上路,请多包涵

我找不到在 VueJS 中格式化数字的方法。我发现的只是用于格式化货币的 内置货币过滤器vue-numeric ,需要进行一些修改才能看起来像一个标签。然后你不能用它来显示迭代的数组成员。

原文由 Buffalo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 467
2 个回答

安装 numeric.js

 npm install numeral --save

定义自定义过滤器:

 <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>

用它:

 <tr v-for="(item, key, index) in tableRows">
  <td>{{item.integerValue | formatNumber}}</td>
</tr>

原文由 Buffalo 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题