import java.text.DecimalFormat;
import java.text.NumberFormat;public class NumFormat
{ public static void main(String args[])
{
System.out.println(percent(0.2341));
}
public static String percent(double dblNum)
{
NumberFormat ft = new DecimalFormat("#,##0.00");
return ft.format(dblNum*100)+"%";
}
}
import java.text.NumberFormat;public class NumFormat
{ public static void main(String args[])
{
System.out.println(percent(0.2341));
}
public static String percent(double dblNum)
{
NumberFormat ft = new DecimalFormat("#,##0.00");
return ft.format(dblNum*100)+"%";
}
}
int y=3;
int f = 2;//小数点后要保留位数int a = x*100/y;
int b = ((x*100%y)*pow(10,f))/y;
int c = ((x*100%y)*pow(10,f))%y;
if(c>=5) b +=1;String str = a+"."+b+"%";最后str就是你要的形式了
改成
int c = (((x*100%y)*pow(10,f))%y)/y;顺便:用float类型比较好
double a=2d/3d;
df.applyPattern("#.00%");
df.format(a);
System.out.println(df.format(a));
liaoqingpeng(棋快一步) 和 thinkerhj(布恩特) 介绍的方法很好用,不错!
micromai(micromai) 的就繁一点点。
:)
DecimalFormat df=new DecimalFormat("#0.00%");
double a=2d/3d;
System.out.println(df.format(a));