js calculate percentage between two integers

1      // Calculate the percentage between two integers 
2      // curNum: current data, totalNum: total data, isHasPercentStr: whether to return a % character 
3      function getPercent(curNum, totalNum, isHasPercentStr) {
 4          curNum = parseFloat(curNum);
 5          totalNum = parseFloat(totalNum);
 6  
7          if (isNaN(curNum) || isNaN(totalNum)) {
 8              return '-' ;
 9          }
 10  
11          return isHasPercentStr ?
 12              totalNum <= 0 ? '0%' : (Math. round(curNum / totalNum * 10000) / 100.00 + '%' ) :
 13             totalNum <= 0 ? 0 : (Math.round(curNum / totalNum * 10000) / 100.00);
14     }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325068621&siteId=291194637