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 sample code for online preview of office files

I'm working on electronic archives recently, ...

JavaScript CollectGarbage Function Example

First, let's look at an example of memory rel...

Press Enter to automatically submit the form. Unexpected discovery

Copy code The code is as follows: <!DOCTYPE ht...

A general method for implementing infinite text carousel with native CSS

Text carousels are very common in our daily life....

How to set an alias for a custom path in Vue

How to configure custom path aliases in Vue In ou...

Solution to the paging error problem of MySQL one-to-many association query

The query data in the xml price inquiry contains ...

Use of Linux ln command

1. Command Introduction The ln command is used to...

A Deeper Look at the Differences Between Link and @import

There are three main ways to use CSS in a page: ad...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

Detailed analysis of MySQL optimization of like and = performance

introduction Most people who have used databases ...

Conflict resolution when marquee and flash coexist in a page

The main symptom of the conflict is that the FLASH...

How to use linux commands to convert and splice audio formats

Install FFmpeg flac eric@ray:~$ sudo apt install ...

Good website copywriting and good user experience

Looking at a website is actually like evaluating a...

Docker stop stops/remove deletes all containers

This article mainly introduces Docker stop/remove...