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));
}
}