2016-07-14 8 views
0

이 문제에 대한 해결책을 찾으려고했지만 어떻게 질문해야하는지 확신 할 수 없습니다.

TextView에 단어와 숫자를 채우려면 Activity에 설정해야합니다.

 float leftTimeSecs = leftTime/1000; 
     float roundedLeftSecs = round(leftTimeSecs, 3); 
     //How to access this directly from "strings" or .xml files 
     duration.setText(getString(R.string.time_data_sec, roundedLeftSecs, 
       getString(R.string.seconds))); 

나는 텍스트를 설정 내 문자열에 있습니다 : (인

<string name="time_data_sec">Duration: %1$f %2$s</string> 

및 호출 된 라운드 기능은

public static float round(float value, int places) { 
     if (places < 0) throw new IllegalArgumentException(); 
     BigDecimal bigDecimal = new BigDecimal(value); 
     bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP); 
     return bigDecimal.floatValue(); 
    } 

내가 출력이 roundedLeftSecs에서 값을 인쇄 할 것입니다 leftTime은 3 자리로 반올림되었습니다.)하지만 roundedLeftSecs를 하드 코드하지 않으면 반올림 된 숫자 뒤에 많은 후행 0이 생성됩니다.

% 1 $ d는 소수 자리 표시 자이지만 float 또는 double 표현식으로 사용할 수 없으므로이 0을 제거하는 방법이나 가능하지 않은지 확실하지 않습니다.

답변

0

같은 문제가있는 사람들을 위해 % 1 $ s을 사용하고 내 둥근 수를 문자열로 변환하여 완벽하게 해결했습니다.

<string name="time_data_sec">Duration: %1$s %2$s</string> 
: 여기
float leftTimeSecs = leftTime/1000; 
       float roundedLeftSecs = round(leftTimeSecs, 3); 
       String roundedLeftSecString = Float.toString(roundedLeftSecs); 
       duration.setText(getString(R.string.time_data_sec, roundedLeftSecString, 
         getString(R.string.seconds))); 

문자열입니다 : 내가 반올림 싶었던 여기

입니다