开发者

A way to round Floats down

Float round rounds it up or down. I always need it to round down.

I have the solution but i dont really like it... Maybe there is a better way.

This is what i want:

1.9999.round_down(2) 
#=> 1.99
1.9901.round_down(2)
#=> 1

I came up with this solution but i would like to know if there is a better solution(I dont like that i convert the float twice). Is there already a method for this? Because I found it pretty strange that I couldnt find it.

class Float
  def round_down(n开发者_如何学JAVA=0)
    ((self * 10**n).to_i).to_f/10**n 
  end
end

Thanks.


1.9999.to_i
#=> 1
1.9999.floor
#=> 1

answered 1 sec ago fl00r

"%.2f" % 1.93213
#=> 1.93

@kimmmo is right.

class Float
  def round_down(n=0)
    self.to_s[/\d+\.\d{#{n}}/].to_f
  end
end


Based on answer from @kimmmo this should be a little more efficient:

class Float
  def round_down n=0
  s = self.to_s
  l = s.index('.') + 1 + n
  s.length <= l ? self : s[0,l].to_f
  end
end

1.9991.round_down(3)
 => 1.999
1.9991.round_down(2)
 => 1.99
1.9991.round_down(0)
 => 1.0
1.9991.round_down(5)
 => 1.9991

or based on answer from @steenslag, probably yet more efficient as there is no string conversion:

class Float
  def round_down n=0
    n < 1 ? self.to_i.to_f : (self - 0.5 / 10**n).round(n)
  end
end


Looks like you just want to strip decimals after n

class Float
  def round_down(n=0)
    int,dec=self.to_s.split('.')
    "#{int}.#{dec[0...n]}".to_f
  end
end


1.9991.round_down(3)
 => 1.999
1.9991.round_down(2)
 => 1.99
1.9991.round_down(0)
 => 1.0
1.9991.round_down(10)
 => 1.9991

(Edit: slightly more efficient version without the regexp)


You could use the floor method

http://www.ruby-doc.org/core/classes/Float.html#M000142


For anybody viewing this question in modern times (Ruby 2.4+), floor now accepts an argument.

> 1.9999.floor(1)
 => 1.9
> 1.9999.floor(2)
 => 1.99
> 1.9999.floor(3)
 => 1.999
> 1.9999.ceil(2)
 => 2.0


In Ruby 1.9:

class Float
  def floor_with_prec(prec = 0)
    (self - 0.5).round(prec)
  end
end


class Float
  def rownd_down(digits = 1)
    ("%.#{digits+1}f" % self)[0..-2].to_f
  end 
end

> 1.9991.rownd_down(3)
=> 1.999
> 1.9991.rownd_down(2)
=> 1.99
> 1.9991.rownd_down(10)
> 1.9991


Found a bug for the answers that try to calculate float in round_down method.

> 8.7.round_down(1)
=> 8.7
> 8.7.round_down(2)
=> 8.69

you can use bigdecimal, integer or maybe string to do all the math but float.

 > 8.7 - 0.005
 => 8.694999999999999    

Here is my solution:

require 'bigdecimal'

class Float
  def floor2(n = 0)
    BigDecimal.new(self.to_s).floor(n).to_f
  end
end

> 8.7.floor2(1)
=> 8.7
> 8.7.floor2(2)
=> 8.7

> 1.9991.floor(3)
=> 1.999
> 1.9991.floor(2)
=> 1.99
> 1.9991.floor(1)
=> 1.9


class Float
  def round_down(n)
    num = self.round(n)
    num > self ? (num - 0.1**n) : num
  end
end

56.0.round_down(-1) = 50. Works with negative numbers as well, if you agree that rounding down makes a number smaller: -56.09.round_down(1) = -56.1.


Found this article helpful: https://richonrails.com/articles/rounding-numbers-in-ruby

Here are the round up and down methods:

class Float
  def round_down(exp = 0)
   multiplier = 10 ** exp
   ((self * multiplier).floor).to_f/multiplier.to_f
  end

  def round_up(exp = 0)
   multiplier = 10 ** exp
   ((self * multiplier).ceil).to_f/multiplier.to_f
  end
end


This worked for me.

> (1.999).to_i.to_f

For rounding up you could just use

> (1.999+1).to_i.to_f
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜