2016-09-25 84 views
0

嘗試將釐米轉換爲英寸,然後四捨五入到最接近的半英寸並打印出一個小數點。以釐米爲單位將高度轉換爲英寸,但無法獲取小數點

3.1 = 3.0 
3.2 = 3.0 
3.3 = 3.5 
3.6 = 3.5 
3.8 = 4.0 

float index; 
float height; 

index = (Math.round((height * .393701)*2))/2; 

text.setText("Index: " + index); 

當我打印索引時,它不會顯示小數。一旦數字達到.75或更高,它會輪到下一個更高的整數。

回答

0

我把一個F在每個號的末尾。

(Math.round((height/2.54f)*2f)/2f) 

身高

147/2.54 = 57.874016 = 58 
148/2.54 = 58.26772 = 58.5 
0

試試這個,

float index; 
float height; 

index = (float) (Math.round((height/2.54) * 10)/10.0); 

text.setText("Index: " + index); 
+0

這把小數點在結束但不四捨五入到最接近的半英寸。我從147釐米開始,它將它轉換成.5,從.1 - .6 - .1等開始增加 – th3ramr0d

相關問題