0
How to convert float into feet-inches (ex:1-2 1/2) in Python?
7 Respuestas
+ 3
Jay Matthews
import math
import fractions
def f1(a):
x=round(a*16)
y=math.modf(x/16)
f=int(y[1]//12)
i=int(y[1]%12)
fr=fractions.Fraction(a[0])
After this, I could not proceed further.
+ 1
My bad. We use the metric system here in Argentina, so I needed an explanation.
Give me a few minutes and I will do my best.
+ 1
This is a working example. You can fine-tune the output format. If the input can be any arbitrary float, one has to make some assumptions about the acceptable approximation of the result, so I rounded to 2 decimals.
https://code.sololearn.com/ck9UTNJFy1ph/?ref=app
0
https://code.sololearn.com/cTX7UsoQoA6H/?ref=app
Tell me if this works. I found it written in Java and translated it to Python.
0
I've done it...but...It get kind of ugly unless the number is multiples of 0.25.
34.25 or 34.50 or 34.75 would output as required...but 34.26...oh dear...ugly.
What are your expected input?
@Fernando Pozzetti
as I understand it..I think he/she want:-
input = 25.25 (inches)
output = 2 feet, 1 and 1/4 inches
0
Fernando Pozzetti This is not what I am looking for. As rodwynnejones mentioned, if input is, 13.50, the output should be 1-1 1/2.
0
Nope, I have tried but I can´t sort it out.
Anyway, I have never heard the idea of "half a foot". For example, something is said to be "2 feet, 5 inches".
We can transform a foot to inches by dividing it by 12.
1 foot = 12 inches.
We can do the opposite to get a foot from inches. However, if we do 6 inches, it will be 0.5 feet, defeating the whole purpose of the concept of the imperial system, which is more complicated than our loved metric system. 6 inches is not half a foot, it is SIX INCHES.
I don´t know why would you need such a tool either. Lengths are in fractions of an inch, but not in fractions of a foot, because an inch is already a fraction of a foot.
However, I wonder what creative ideas this might create.