当前位置:首页 » 编程语言 » java求百分比

java求百分比

发布时间: 2025-06-15 22:35:48

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

热点内容
mssql删除存储过程 发布:2025-07-31 11:20:36 浏览:570
产线数据库 发布:2025-07-31 11:18:18 浏览:270
mysql数据库安装不了 发布:2025-07-31 11:07:34 浏览:777
java图片解析 发布:2025-07-31 11:06:49 浏览:777
asp无组件上传代码 发布:2025-07-31 10:56:03 浏览:905
爱玩源码网 发布:2025-07-31 10:56:02 浏览:483
如何知道内网中服务器地址 发布:2025-07-31 10:54:37 浏览:385
爱豆怎么缓存视频 发布:2025-07-31 10:52:48 浏览:825
如何获知自家wifi密码 发布:2025-07-31 10:51:14 浏览:412
15人团队解压拓展多少钱 发布:2025-07-31 10:30:36 浏览:804