java求百分比
㈠ java怎麼計算百分比
JAVA計算百分比參考以下操作:
intnum1=7;
intnum2=9;
//創建一個數值格式化對象
NumberFormatnumberFormat=NumberFormat.getInstance();
//設置精確到小數點後2位
numberFormat.setMaximumFractionDigits(2);
Stringresult=numberFormat.format((float)num1/(float)num2*100);
System.out.println("num1和num2的百分比為:"+result+"%");
㈡ java計算百分比的代碼怎麼寫
public String getPercent(int x,int total){
String result="";//接受百分比的值
double x_double=x*1.0;
double tempresult=x/total;
//NumberFormat nf = NumberFormat.getPercentInstance(); 注釋掉的也是一種方法
//nf.setMinimumFractionDigits( 2 ); 保留到小數點後幾位
DecimalFormat df1 = new DecimalFormat("0.00%"); //##.00% 百分比格式,後面不足2位的用0補齊
//result=nf.format(tempresult);
result= df1.format(tempresult);
return result;
}
====================
idehub提供雲端手機、平板、pc瀏覽器等方式的java編程平台,讓你隨時隨地可以編寫java代碼,並有大神在線解答所有技術問題!關注code4a微信公眾號!
㈢ 怎樣用JAVA計算百分比,比如我做了一個HTTP測試程序,需要在一段時間內計算連接成功和失敗的百分比,怎麼
public double fun(int a,int b){
//a表示成功次數,b表示失敗次數,c表示成功百分比,d表示失敗百分比
double c=(a/(a+b))*100;
double d=(b/(a+b))*100;
//返回成功百分比
return c;
//返回失敗百分比
//return d;
}
得到的是一個double類型的數據,具體保留幾位小數啊什麼的自己處理一下就可以了。
㈣ java中表示百分比一般用什麼符號 是irem嗎
java中表示百分比一般用什麼符號 用%,
bipush 100 把數100(位元組類型)放到棧上
irem 從棧上取2個元素做求余運算
bipush 30 把數30(位元組類型)放到棧上
㈤ java百分比計算
public String getPercent(Integer num,Integer total ){
String percent ;
Double p3 = 0.0;
if(total == 0){
p3 = 0.0;
}else{
p3 = num*1.0/total;
}
NumberFormat nf = NumberFormat.getPercentInstance();
nf.setMinimumFractionDigits(2);//控制保留小數點後幾位,2:表示保留2位小數點
percent = nf.format(p3);
return percent;
}
㈥ java中如何計算百分比之後保留指定
double passPercent=pass/(double)score.length;
double excellentPercent=excellent/(double)score.length;
//獲取格式化對象
NumberFormat nt=NumberFormat.getPercentInstance();
nt.setMinimumFractionDigits(2);
System.out.println("及格率為:"+nt.format(passPercent));
System.out.println("優秀率為:"+nt.format(excellentPercent));
㈦ Java編程里百分比算術怎麼編寫的
你意思是不是這樣的,兩個數相除,得到結果不是以小數形式,而是用百分號形式來顯示的呢?
如果是這樣,則代碼如下
public class A1
{
public static void main(String args[]) {
NumberFormat PercentFormat = NumberFormat.getPercentInstance();
double a = 4;
double b = 5;
System.out.println(PercentFormat.format(a/b));
}
}