A way to round Floats down

耗尽温柔 提交于 2019-12-19 05:16:11

问题


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=0)
    ((self * 10**n).to_i).to_f/10**n 
  end
end

Thanks.


回答1:


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



回答2:


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



回答3:


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)




回答4:


You could use the floor method

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




回答5:


In Ruby 1.9:

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



回答6:


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



回答7:


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



回答8:


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.




回答9:


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



回答10:


This worked for me.

> (1.999).to_i.to_f

For rounding up you could just use

> (1.999+1).to_i.to_f


来源:https://stackoverflow.com/questions/7090537/a-way-to-round-floats-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!