Filter the numbers in the data into units for display in Vue

vue filter

I wrote this article because I encountered this problem during the development process. There was a small bug. It was necessary to convert the value passed from the background 2364828to the ending unit. I tried to check it online and tried several Netizens couldn't share the method, but later I saw a post and tried it and it succeeded. Here I will talk about the problem and the solution in detail.

problem

You can see that what I display on the web page is a long string of numbers
Insert picture description here
Bound data value

Solution
<div class="count">
        <div>
          <img src="~assets/img/content/erji.svg" alt />
       <span> {
   
   {item.playCount| playvolume}} </span>
        </div>

JavaScript code

<script>
export default {
    
    
	data() {
    
    
		return {
    
    				};	
	},
	
	onLoad() {
    
    },

	//这里的单位可以根据你自己的需求做更改可以改成千、万等等
	//后面的.toFixed为保留两位小数
	
	filters:{
    
    
		//取截单元,单位
		playvolume:function(arg){
    
    
			if(arg.toString().length>=13){
    
    
				// return arg/1000000000000+"万亿"
				const volume= arg/1000000000000
				const realVal = parseFloat(volume).toFixed(2);
				return realVal+"万亿"
				
			}else if(arg.toString().length>=9){
    
    
				const volume= arg/100000000
				const realVal = parseFloat(volume).toFixed(2);
				return realVal+"亿"
			}else if(arg.toString().length>=4){
    
    
				const volume= arg/10000
				const realVal = parseFloat(volume).toFixed(2);
				return realVal+"万"
			}
			
		}
	}
};
</script>

<style>
</style>



Effect picture
Insert picture description here
I changed .toFixed(0) to 0 here, without keeping the decimal point.

Reprint link: https://blog.csdn.net/qq_34596127/article/details/108263964

.toFixed specific usage: https://www.w3school.com.cn/js/jsref_tofixed.asp

Guess you like

Origin blog.csdn.net/weixin_45054614/article/details/112501505