2012-10-02 2 views

답변

6

int 분할이 없으므로 항상 int가 잘립니다. 대신 부서에서 최소한 하나의 이중 값을 사용하도록하십시오.

double num = 4.0/3.0; 

그럼 당신은 당신이 당신의 진수 장소를 선택할 수 있도록, 문자열로 표시 출력을 포맷 할 때

// one way to do it 
// %.3f is for a floating number with 3 digits to the right of the decimal 
// %n is for new line 
System.out.printf(%.3f%n, num); 

Another: 
DecimalFormat decimalFormat = new DecimalFormat("0.000"); 
System.out.println(decimalFormat.format(num)); 
+0

OMG 서식을 !!!!!!!!!!!!!!!!!!!! 감사합니다 T_T – Jcorretjer

+1

그럼 당신은 4.0/3 또는 4/3.0과 같은 두 가지가 있어야합니다.하지만 둘 다 다칠 수는 없습니다. +1 –

+1

@Alex : 옙,'(double) 4/3'은 잘 작동합니다. –

0

또한 및 String.format를 사용한다 (%의 1.2f, yourDouble) 당신의 소수점 이하 자릿수

+3

예,이 코드는 이중 데이터를 출력하지만'int/int = int'를 확인하므로'4/3'은'1'이 될 것이고, '1.333'을 얻기 위해서는'4.0/3.0'을 사용해야합니다. –

0

이 시도 ..

double value= 255.956666; 

BigDecimal bd = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP); 
System.out.println("value="+bd); 
관련 문제