2016-09-01 5 views
1

내 스크립트에서 반올림 코드가 두 배로되는 문제가있어 오류를 찾을 수 없습니다. 문제가 발생하는 곳을 찾을 수없는 것 같습니다. 모든 의견을 주시면 감사하겠습니다.반올림 코드의 배가 오류

def research_rounding(first_time): 
    first_time = float(first_time) 
    if first_time == 0:      #If time is 0, return 0 
     result_time = 0 
     return result_time 

    else:         #If time is not 0, continue rounding func 
     first_time = str(first_time) 
     find_dec = "." 
     length = len(first_time) 
     found_dec = (first_time.find(find_dec)) 
     found_whole = float(first_time[0:found_dec]) 
     found_tenth = float(first_time[found_dec:length]) 

     if found_tenth < 0.25 and found_whole >= 1 and found_tenth != 0: 
      result_time = 0.25 + found_whole 
      return result_time 
     elif found_tenth == 0 and found_whole >=1: 
      result_time = 0 + found_whole 
      return result_time 
     elif found_tenth == 0 and found_whole == 0: 
      result_time = 0 
      return result_time 
     elif found_tenth <= 0.5: 
      result_time = 0.5 + found_whole 
      return result_time 
     elif found_tenth > 0.5 and found_tenth < 0.75: 
      result_time = 0.75 + found_whole 
      return result_time 
     elif found_tenth > 0.75 and found_tenth < 1: 
      result_time = 1 + found_whole 
      return result_time 
     else: 
      pass 
+3

무슨 문제입니까? 예상 결과와 현재 결과가 포함 된 예제를 추가하십시오. – Daniel

답변

1

당신은 가장 가까운 1/4로 반올림하려고하는 가정은, 오히려 문자열 장난보다, 수치 그렇게 훨씬 더 쉽게 (그리고 더 정확)입니다.

import math 
result_time = math.floor(4*first_time + .5)/4 

Floor()는 음의 무한대를 자릅니다. 따라서이 접근법은 first_time이 음수 일 때도 작동합니다.