2012-08-12 5 views
1

안녕하세요, 저는 TextView 수준에서 십진법을 적용 할 수있게 만들고 싶습니다. 그러나 어떻게해야 할 지 알 것입니다. 지금은 1을 내고 있지만 1.80을 내고 싶습니다. :)텍스트보기로 소수점을 표시하십시오.

public class Main extends Activity { 

int counter; 
EditText weight, hours; 
TextView amount, level; 
Button calcuate; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    counter = 0; 
    weight = (EditText) findViewById(R.id.weight); 
    hours = (EditText) findViewById(R.id.hours); 
    amount = (TextView) findViewById(R.id.amount); 
    level = (TextView) findViewById(R.id.alcohol_level); 
    calcuate = (Button) findViewById(R.id.calcuate); 


    final String widmark = getResources().getString(
      R.string.widmark); 
    final String hundra = getResources().getString(
      R.string.hundra); 
    final String cl = getResources().getString(
      R.string.cl); 

    calcuate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Integer wid, mgs; 

      String w = weight.getText().toString(); 
      String h = hours.getText().toString(); 

      wid = Integer.parseInt(w) * Integer.parseInt(widmark)/Integer.parseInt(hundra); 
      mgs = Integer.parseInt(cl)/Integer.parseInt(wid.toString())/Integer.parseInt(hundra); 

      level.setText(mgs.toString()); 
     } 
    }); 

} 

} 
+0

arithmatic을 수행하기 위해 부동 소수점 숫자를 사용하고 원하는 정밀도가 유지되도록 결과의 형식을 지정하려면'DecimalFormat '을 사용해야합니다. – neevek

답변

0

int 부서는 int 님의 작품입니다. 출력이 float 또는 double 유형이되도록하려면 double 또는 float division을 사용해야합니다. 없는 표현에서 고려 모든 변수가 double 년대, 그 중 하나를 수행 할 필요가

Double mgs; 

mgs = Double.parseDouble(cl)/Double.parseDouble(wid.toString())/Double.parseDouble(hundra); 

참고.

또 다른 주목할만한 점은 여기에 통화를 가정 할 때 소수점 두 자리를 원하는지 여부입니다. 기본적으로 Java/Android는 필요한만큼 소수 자릿수를 출력합니다. 1.801.8으로 표시됩니다. 이를 완화하려면 (특히 NumberFormat.getCurrencyInstance())을 사용하여 기본값 인 Locale의 통화에 대해 소수 자릿수를 지정하도록 지정할 수 있습니다.

+0

빠른 anwser에 감사드립니다. D – user1475475

1

mgs 변수는 정수 개체입니다. 소수점 이하 자릿수를 표시하려면 float를 입력하십시오.

float mgs = Integer.parseInt(cl)/Integer.parseInt(wid.toString())/Integer.parseInt(hundra); 

이 정보가 도움이되기를 바랍니다.

관련 문제