Vue implements the digital thousands separator format globally

Vue implements the digital thousands separator format globally

This article example shares the specific code for Vue to globally implement the digital thousands separator format for your reference. The specific content is as follows

What does this mean? When we need to render data on the page, for example, 88888, we need to display it in a format that is easy to read, 88,888, according to the thousandths.

At this time, what I do is to write a filter in vue and filter all the data with this filter.

Because the data involved is relatively large, I mounted this filter globally so that it does not need to be referenced on each page.

Conversion code implementation

First, create a file numberToCurrency.js to implement the conversion function of the thousand separator of numbers.

export function numberToCurrencyNo(value) {
  if (!value) return 0
  // Get the integer part const intPart = Math.trunc(value)
  // Integer part processing, increase,
  const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
  // Predefined decimal part let floatPart = ''
  //Truncate the value into decimal and integer parts const valueArray = value.toString().split('.')
  if (valueArray.length === 2) { // There is a decimal part floatPart = valueArray[1].toString() // Get the decimal part return intPartFormat + '.' + floatPart
  }
  return intPartFormat + floatPart
}

Okay, this is achieved. Of course, if there are other requirements, the specific conversion code needs to be modified according to the actual situation.

Next comes the citation.

Reference mount global

Import the filter file just now into the main.js file and mount it globally.

import { numberToCurrencyNo } from '@/utils/numberToCurrency'
// Configure the global filter to implement the digital thousandths format Vue.filter('numberToCurrency', numberToCurrencyNo)

That's it, and then you can use it where you need to convert.

use

If you use it, it is the same as the normal filter.

<p class="num color1">{{riskAll| numberToCurrency}}</p>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Summary of Vue interpolation, expression, separator, and instruction knowledge

<<:  Detailed explanation of putting common nginx commands into shell scripts

>>:  XHTML Tutorial: The Difference Between Transitional and Strict

Recommend

vue.config.js packaging optimization configuration

The information on Baidu is so diverse that it...

Vue implements carousel animation

This article example shares the specific code of ...

A quick review of CSS3 pseudo-class selectors

Preface If CSS is the basic skill of front-end de...

Introduction to the functions and usage of value and name attributes in Html

1. The value used in the button refers to the text...

Share a Markdown editor based on Ace

I think editors are divided into two categories, ...

How to solve the phantom read problem in MySQL

Table of contents Preface 1. What is phantom read...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

In-depth understanding of MySQL various locks

Table of contents Lock Overview Lock classificati...

Centos7 implements sample code for restoring data based on MySQL logs

Introduction Binlog logs, that is, binary log fil...

mysql calculation function details

Table of contents 2. Field concatenation 2. Give ...

HTML input file control limits the type of uploaded files

Add an input file HTML control to the web page: &...

How to view server hardware information in Linux

Hi, everyone; today is Double 12, have you done a...

HTML imitates Baidu Encyclopedia navigation drop-down menu function

HTML imitates the Baidu Encyclopedia navigation d...