java前后台計算百分比(保留2位小數)


1.后台計算百分比:

**方法一:

  public String getPercent(Integer num,Integer totalPeople ){
    String percent ;
    Double p3 = 0.0;
    if(totalPeople == 0){
      p3 = 0.0;
    }else{
      p3 = num*1.0/totalPeople;
    }
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMinimumFractionDigits(2);//控制保留小數點后幾位,2:表示保留2位小數點
    percent = nf.format(p3);
    return percent;
  }


**方法二:

  public static String MoneyShowFormat(double money){
    DecimalFormat df=new DecimalFormat("#0.00");
    return df.format(money+0.000001d);
  }


2.jquery計算百分比:
  

**方法一:

  var str = "123";
  var totalPeople2 = parseInt(str);//str-->int
  var num = 5;
  var percent = (totalPeople2/num)*100
  percent = Math.round(percent*100)/100; //保留2位小數


**方法二:

  percent = percent.toFixed(2) //2保留小數點后2位

**方法三:

  var percentStr = percent + "";
  var str = s.substring(0,s.indexOf(".") + 3);

**方法四:

  var percentStr = percent + ""; //未驗證
  var re = /([0-9]+\.[0-9]{2})[0-9]*/; //正則表達式
  aNew = percentStr.replace(re,"$1");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM