java计算百分比的代码怎么写?

double x_double=x*1.0;double tempresult=x/total;//NumberFormat nf = NumberFormat.getPercentInstance(); 注释掉的也是一种方法 //nf.setMinimumFractionDigits( 2 ); 保留到小数点后几位 DecimalFormat df1 = new DecimalFormat("0.00%"); //##.00% 百分比格式,后面不...
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微信公众号!2015-09-06
//方法1public static String accuracy(double num, double total, int scale){ DecimalFormat df = (DecimalFormat)NumberFormat.getInstance(); //可以设置精确几位小数 df.setMaximumFractionDigits(scale); //模式 例如四舍五入 df.setRoundingMode(RoundingMode.HALF_UP); double accuracy_num = num / total * 100; return df.format(accuracy_num)+"%";} int num = 1; int total = 100; int scale = 1; String result = accuracy(num, total, scale); System.out.println(result);//1% int num = 1; int total = 3; int scale = 1; String result = accuracy(num, total, scale); System.out.println(result);//33.3% int num = 1; int total = 1000; int scale = 1; String result = accuracy(num, total, scale); System.out.println(result);//0.1% int num = 1; int total = 10000; int scale = 1; String result = accuracy(num, total, scale); System.out.println(result);//0%//方法二//0表示的是小数点 之前没有这样配置有问题例如 num=1 and total=1000 结果是.1 DecimalFormat df = new DecimalFormat("0%"); //可以设置精确几位小数 df.setMaximumFractionDigits(1); //模式 例如四舍五入 df.setRoundingMode(RoundingMode.HALF_UP); int num = 1; int total = 1000; double accuracy_num = num * 1.0/ total * 1.0; System.out.println(df.format(accuracy_num));2021-01-06
mengvlog 阅读 13 次 更新于 2025-06-20 06:14:50 我来答关注问题0
  • 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 ); 保留到小数点后几位 DecimalF...

  • int sum = 0;for (int i = 0; i < arr.size(); i++) { sum += arr.get(i);} 在计算出总和后,可以进一步计算百分比,例如计算某个数字占总和的百分比:double percentage = (double) a / sum * 100;最后,将计算出的百分比输出:System.out.println("数字 " + a + " 占总和的...

  •  Diablogs 怎样用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怎么计算百分比?

    JAVA计算百分比参考以下操作:int num1 = 7; int num2 = 9; // 创建一个数值格式化对象 NumberFormat numberFormat = NumberFormat.getInstance(); // 设置精确到小数点后2位 numberFormat.setMaximumFractionDigits(2); String result = numberFormat.format((float) num1 / (float) num2 ...

  •  翡希信息咨询 请问在JAVA中我用DecimalFormat来计算百分比,有些百分比相加不等于100%,怎么办?

    在Java中使用DecimalFormat来计算百分比时,如果有些百分比相加不等于100%,可以尝试以下方法解决:设置四舍五入模式:DecimalFormat默认可能不是使用四舍五入的方式进行取值。为了确保计算的准确性,你可以通过设置RoundingMode为HALF_UP来启用四舍五入模式。例如:javaDecimalFormat df = new DecimalFormat;df....

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部