how round number nearest 9 in ruby?
my desired effect if number 22.22 round down 19. if number 26.34 round 29
any ideas?
thanks
unfortunately, round() methods float don't have syntactic sugar rounding other nearest integer. 1 can build one's own methods—
this method assumes numbers positive. readability, advise wrap convoluted numerical calculation method name refers it's doing.
def round_to_nearest_9(num) ((num + 1) / 10).round * 10 - 1 end if don't want assume input number positive:
def round_to_nearest_9(num) if num < 0 return ((num - 1) / 10).round * 10 + 1 else return ((num + 1) / 10).round * 10 - 1 end end
Comments
Post a Comment