#!/usr/bin/python3 # =================================================================== # convert float degrees to deg/min/sec # =================================================================== import user_interface as ui # ---- array of float test data (deg + min + sec) test_data = [ 30.263888889, 180.0, 90.0 + 10/60.0, 36.0 + 45/60.0 + 32.8/3600.0, 721.0, -(270.0 + 52/60.0) ] # ------------------------------------------------------------------- # ---- convert to degrees:int, minutes:int, seconds:int, seconds:float # ---- (function does not work with negative degrees - fix it) # ------------------------------------------------------------------- def dms(deg:float) -> tuple: if deg < 0.0: print('degrees must not be negative') return (0,0,0,0.0) d = int(deg) mf = abs(deg - d) * 60.0 m = int(mf) sf = (mf - m) * 60.0 s = int(sf) return (d,m,s,sf) # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- while True: ## ---- test with user input ## ##print() ##s = ui.get_user_input('Input degrees: ') ## ##if not s: # empty string? ## break ## ##tf,deg = ui.is_float(s) ## ##if not tf: ## print() ## print(f'bad input ({s})') ## continue ## ##d,m,s,sf = dms(deg) ## ##print() ##print(f'converted: {d}/{m}/{s} {d}/{m}/{sf}') # ---- test with predefined data for x in test_data: print('------------------------------------------') print(f'degrees: {x:.6}') d,m,s,sf = dms(x) print(f'converted: {d}/{m}/{s} {d}/{m}/{sf:.6f}') break print()