shivam sharma
shivam sharma

Reputation: 3

float value to time(HH:MM) FORMAT

hr = int(input("Enter period of earth rotation : "))

ln = float(input("Enter value of longitude : "))

calc = (hr/360)*ln

print (calc)

INPUT:- 24,82.50

I expect the output to be 5:30, instead of 5.5

Upvotes: 0

Views: 56

Answers (2)

GOVIND DIXIT
GOVIND DIXIT

Reputation: 1828

Try this:

hr = int(input("Enter period of earth rotation : "))

ln = float(input("Enter value of longitude : "))

calc = (hr/360)*ln
hours = int(calc)
minutes = (calc*60) % 60

print("%d:%02d" % (hours, minutes))

Output

Enter period of earth rotation : 24
Enter value of longitude : 82.50
5:30

Upvotes: 0

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

You've already got calc, the exact time in hours, and what you need is to convert the decimal to some number of minutes. I'd do this by storing hours and minutes separately:

hours = int(calc)
minutes = int((calc - hours) * 60)
print(f"{hours}:{minutes:02}")

Upvotes: 2

Related Questions