将十进制英尺转换为英尺和英寸?

时间:2010-01-26 03:40:26

标签: android decimal

如何将测量值从12.5英尺转换为12英尺6英尺?如何创建第二个数字,当乘以12时只读取小数位?

现在我有两个Measurement01使用其他变量和一些数学来得到小数点的脚。我将它发送到带有farf.setText的textview(Measurement01 +“”+“ft”);

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:7)

减去整数部分:

float measurement = 12.5;
int feet = (int)measurement;
float fraction = measurement - feet;
int inches = (int)(12.0 * fraction);

答案 1 :(得分:1)

很简单,length是浮点长度:

int feet = (int)length;
int inches = (length - feet) * 12.0;
: :
farf.setText (feet + " ft, " + inches + " inches");

答案 2 :(得分:0)

以@ Ry4an的答案为基础:

//... Other code above

float Measurement01 = 12.5;
int feet = (int)Measurement01;
float fraction = Measurement01 - feet;
int inches = (int)(12.0 * fraction);

// Display like: 2.5 = 2 ft 6 in, 0.25 = 3 in, 6.0 = 6 ft
farf.setText((0 != feet ? feet + " ft" : "") + (0 != inches ? " " + inches + " in" : ""));