【发布时间】:2016-08-26 18:49:06
【问题描述】:
我在 array.xml 中有两个数组,一个数组包含以英尺英寸为单位的值,另一个数组包含以厘米为单位的相应值。我希望当用户以英尺英寸为单位选择一些值时,我可以以厘米为单位显示相应的值。 现在我正在尝试使用标准公式进行转换,但这并没有给我确切的值。 这是我的代码:
// if user changed the Height from foot inch to centimeters
if(Height_pref.equals(HEIGHT_UNIT)){
CharSequence []changedHeight = changeHeightCms();
heightPicker.setValues(changedHeight);
height_text.setText(getString(R.string.HeightCms));
}
//method to change height from ft'in" to cms
public CharSequence[] changeHeightCms(){
//final double[] weightLbs = new double[200];
final ArrayList<CharSequence> ChangeheightArray = new ArrayList<CharSequence>();
for (int index = 3; index < 7; index++) {
for (int index1 = 0; index1 < 12; index1++) {
float footinch = ((index*12)+index1);
int centimeters = Math.round((float) (footinch*2.54));
ChangeheightArray.add(centimeters + "");
if (index == 6 && index1 == 4) {
break;
}
}
}
CharSequence[] height1 = new CharSequence[ChangeheightArray.size()];
final CharSequence[] HeightCmsArray = ChangeheightArray.toArray(height1);
return HeightCmsArray;
}
任何人都可以告诉我,如果我对这些值进行硬编码,那么我该如何显示转换?
【问题讨论】:
-
但是对于多少英尺的值,您将有相应的厘米?如果你有一些有限的数字,那会很好,但如果有很多,我会建议动态转换方法
-
从英制到公制的转换只是几个乘法和一个加法......为什么要进行表格查找以将英尺/英寸转换为厘米?这闻起来像 XY Problem
-
@vivek 我的值从 3.0 到 7.0
-
@jim 你能告诉我我在哪里做错了转换
-
@GauravTiwari 您一次只转换一个值或多个值。