2016-09-01 7 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 отрицательное число.

 Смежные вопросы

  • Нет связанных вопросов^_^