uniapp计算两个整数之间的百分比

文章描述:

uniapp进度条功能模块计算两个整数之间的百分比

script

方法

//curNum:当前数据,totalNum:总数据,isHasPercentStr:是否返回%字符
getPercent(curNum, totalNum, isHasPercentStr){
	curNum = parseFloat(curNum);
	totalNum = parseFloat(totalNum);
	if (isNaN(curNum) || isNaN(totalNum)) {
		return '-';
	}
	return isHasPercentStr ?
	totalNum <= 0 ? '0%' : (Math.round(curNum / totalNum * 10000) / 100.00 + '%') :
	totalNum <= 0 ? 0 : (Math.round(curNum / totalNum * 10000) / 100.00)
}

调用

var curNum = 81;
var totalNum = 80;
var isHasPercentStr = '';
var str = this.getPercent(curNum, totalNum, isHasPercentStr)
console.log(str)

 

发布时间:2022/05/09

发表评论