/**
* 方式一:使用java.text.NumberFormat实现
* @param x
* @param y
* @return
*/
public static String getPercent(int x, int y) {
double d1 = x * 1.0;
double d2 = y * 1.0;
NumberFormat percentInstance = NumberFormat.getPercentInstance();
// 设置保留几位小数,这里设置的是保留两位小数
percentInstance.setMinimumFractionDigits(2);
return percentInstance.format(d1 / d2);
}
/**
* 方式二:使用java.text.DecimalFormat实现
* @param x
* @param y
* @return
*/
public static String getPercent2(int x, int y) {
double d1 = x * 1.0;
double d2 = y * 1.0;
// 设置保留几位小数, “.”后面几个零就保留几位小数,这里设置保留四位小数
DecimalFormat decimalFormat = new DecimalFormat("##.0000%");
return decimalFormat.format(d1 / d2);
}
版权声明:本文为weixin_49390750原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。