js converts decimals to percentages

In a recent development project, the requirement encountered is to convert the decimal data returned by the background into a percentage. The front-end implementation method is as follows:
If yours is also used by the Vue project, you need to change the definition format of the function.

1. Convert decimals to percentages

1. First convert to number type
2. Then multiply by 100
3. toFixed() function retains several decimal places

	var point = 0.666;
	function toPercent(point){
    
    
		var percent = Number(point*100).toFixed(1);
		percent += "%";
		return percent;
	}
  	 var result = toPercent(point);
	document.write("<br/>"+result);

2. Convert percentages to decimals

1. Remove the percent sign first,
2. Divide by 100,
3. Return out

	var percent = "66.6%";
	function toPoint(percent){
    
    
	 	var str=percent.replace("%","");
		 	str= str/100;
	 	return str;
	}
	toPoint(percent);
	var result = toPoint(percent);
	document.write(result);

Guess you like

Origin blog.csdn.net/weixin_43983960/article/details/121336250