转换小数英尺英尺和英寸?(Convert decimal feet to feet and inch

2019-10-19 06:34发布

我怎么会去从转换的测量,例如,12.5英尺6英寸12英尺? 我将如何创建第二数目由12乘以时只读取小数点的位置?

现在我有使用其他变量和一些数学让我的脚在小数双Measurement01。 我发送与farf.setText一个TextView(Measurement01 + “” + “FT”);

任何帮助,将不胜感激!

Answer 1:

。减去整数部分:

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


Answer 2:

很简单,其中, length为浮点长度:

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


Answer 3:

建立在@ 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" : "")); 


文章来源: Convert decimal feet to feet and inches?