1 import java.text.DecimalFormat;
2 import java.text.NumberFormat;
3
4 import org.junit.Test;
5
6 public class TestCase {
7 /**
8 * 将小数装换成百分比输出
9 * 将double类型保留小数点后两位,转换成
10 */
11 @Test
12 public void test(){
13 // ================================================================================
14 double f =
0.5585;
15 // BigDecimal b = new BigDecimal(f);
16 // double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
17 // System.out.println(f1);
18 System.
out.println(Integer.parseInt(
new DecimalFormat(
"0").format(f*
100))+
"%");
//百分比没有小数点
19 // ===========================首选===================================================
20 double result1=
0.51111122111111;
21 DecimalFormat df =
new DecimalFormat(
"0.00%");
22 String r =
df.format(result1);
23 System.
out.println(r);
//great
24 // =================================================================================
25 NumberFormat num =
NumberFormat.getPercentInstance();
26 num.setMaximumIntegerDigits(
3);
27 num.setMaximumFractionDigits(
2);
28 double csdn =
0.55555555555555555;
29 System.
out.println(num.format(csdn));
//good
30 // =================================================================================
31 double result=
1;
32 int temp = (
int)(result *
1000);
33 result = (
double)temp /
10;
34 System.
out.println(result +
"%");
//100% 变成了 100.0%
35 }
36
37 }
转载于:https://www.cnblogs.com/xiao-fan-fan-2015/p/4775185.html
相关资源:请帮小明同学设计一个程序,输入上次考试成绩(int)和本次考试成绩(int),然后输出成绩提高的百分比,保留两位小数位