1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
String int_rate = "2.2596";
System.out.println("[D] int_rate = " + int_rate);
System.out.println("[D] String double Round1 = " + Double.parseDouble(int_rate)*1000);
System.out.println("[D] String double Round2 = " + Double.parseDouble(int_rate)*1000/1000.0);
System.out.println("[D] String double Round3 = " + Math.round(Double.parseDouble(int_rate)*1000/1000.0));
System.out.println("[D] String double Round4 = " + String.format("%.2f" , Float.parseFloat(int_rate)) );
System.out.println("[D] String double Round5 = " + Double.parseDouble(int_rate)*1000/1000.0);
System.out.println("[D] String double Round6 = " + Math.round(Double.parseDouble(int_rate)*1000/1000.0));
System.out.println("[D] String double Round7 = " + Double.toString(Math.round(Double.parseDouble(int_rate)*1000/1000.0)));
Double add_rate = 3.141592;
add_rate = Math.round(add_rate*1000)/1000.0;
System.out.println("[D] String double Round9 = " + add_rate);
|
cs |
String.format을 사용할 때
String.format("%.2f" + Float.parseFloat(~~); 이렇게도 사용하니
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%.2f' 라는 에러가 나왔다.
파라미터를 찾을 수 없다는 에러. 그래서 ","로 수정하니 잘 됨.
12,13번 line은 double형을 바로 round함수에 넣으면 제대로 출력이 되는데, String을 parseDouble로 변환하고 다시 String으로 변환하니 값이 짤려서 나온다. 실수값이 들어가 있는 String변수는 StringFormat을 써야함!!