Sean
Sean

Reputation: 109

Convert decimal feet to feet and inches?

How would I go about converting a measurement from, for example, 12.5 feet to 12ft 6in? How would I created that second number which reads only the decimal place when multiplying by 12?

right now I have double Measurement01 using other variables and some math to get me the feet in decimals. I send that to a textview with farf.setText(Measurement01 + " " + "ft");

Any help would be appreciated!

Upvotes: 1

Views: 5742

Answers (3)

paxdiablo
paxdiablo

Reputation: 881113

Quite simply, where length is the floating point length:

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

Upvotes: 2

Damon
Damon

Reputation: 687

Building on @Ry4an's answer:

//... 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" : "")); 

Upvotes: 0

Ry4an Brase
Ry4an Brase

Reputation: 78330

Substract the integer portion:

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

Upvotes: 10

Related Questions