我有一个浮点数,我想四舍五入到小数点的百分之一。但是,我只能使用.round,这基本上将其转换为int,即2.34.round # => 2.。
.round
2.34.round # => 2.
djmepvbi1#
传递一个要舍入的参数,该参数包含要舍入到的小数位数
>> 2.3465.round=> 2>> 2.3465.round(2)=> 2.35>> 2.3465.round(3)=> 2.347
>> 2.3465.round
=> 2
>> 2.3465.round(2)
=> 2.35
>> 2.3465.round(3)
=> 2.347
字符串
l2osamch2#
显示时,可以使用(例如)
>> '%.2f' % 2.3465=> "2.35"
>> '%.2f' % 2.3465
=> "2.35"
字符串如果要将其存储为舍入值,可以使用
>> (2.3465*100).round / 100.0=> 2.35
>> (2.3465*100).round / 100.0
型
mcvgt66p3#
你可以用它来四舍五入到一个精确度。
//to_f is for floatsalary= 2921.9121puts salary.to_f.round(2) // to 2 decimal place puts salary.to_f.round() // to 3 decimal place
//to_f is for float
salary= 2921.9121
puts salary.to_f.round(2) // to 2 decimal place
puts salary.to_f.round() // to 3 decimal place
oiopk7p54#
你可以在Float类中添加一个方法,我从stackoverflow中学到了这一点:
class Float def precision(p) # Make sure the precision level is actually an integer and > 0 raise ArgumentError, "#{p} is an invalid precision level. Valid ranges are integers > 0." unless p.class == Fixnum or p < 0 # Special case for 0 precision so it returns a Fixnum and thus doesn't have a trailing .0 return self.round if p == 0 # Standard case return (self * 10**p).round.to_f / 10**p endend
class Float
def precision(p)
# Make sure the precision level is actually an integer and > 0
raise ArgumentError, "#{p} is an invalid precision level. Valid ranges are integers > 0." unless p.class == Fixnum or p < 0
# Special case for 0 precision so it returns a Fixnum and thus doesn't have a trailing .0
return self.round if p == 0
# Standard case
return (self * 10**p).round.to_f / 10**p
end
tvz2xvvm5#
您还可以提供一个负数作为round方法的参数,以舍入到10、100等的最接近倍数。
round
# Round to the nearest multiple of 10. 12.3453.round(-1) # Output: 10# Round to the nearest multiple of 100. 124.3453.round(-2) # Output: 100
# Round to the nearest multiple of 10.
12.3453.round(-1) # Output: 10
# Round to the nearest multiple of 100.
124.3453.round(-2) # Output: 100
8yparm6h6#
(2.3465*100).round()/100.0怎么样?
(2.3465*100).round()/100.0
js4nwp547#
def rounding(float,precision) return ((float * 10**precision).round.to_f) / (10**precision)end
def rounding(float,precision)
return ((float * 10**precision).round.to_f) / (10**precision)
tnkciper8#
如果你只需要显示它,我会使用number_with_precision助手。如果你在其他地方需要它,我会使用,正如Steve Weet指出的,round方法
qqrboqgw9#
对于ruby 1.8.7,你可以在代码中添加以下内容:
class Float alias oldround:round def round(precision = nil) if precision.nil? return self else return ((self * 10**precision).oldround.to_f) / (10**precision) end end end
alias oldround:round
def round(precision = nil)
if precision.nil?
return self
else
return ((self * 10**precision).oldround.to_f) / (10**precision)
9条答案
按热度按时间djmepvbi1#
传递一个要舍入的参数,该参数包含要舍入到的小数位数
字符串
l2osamch2#
显示时,可以使用(例如)
字符串
如果要将其存储为舍入值,可以使用
型
mcvgt66p3#
你可以用它来四舍五入到一个精确度。
字符串
oiopk7p54#
你可以在Float类中添加一个方法,我从stackoverflow中学到了这一点:
字符串
tvz2xvvm5#
您还可以提供一个负数作为
round
方法的参数,以舍入到10、100等的最接近倍数。字符串
8yparm6h6#
(2.3465*100).round()/100.0
怎么样?js4nwp547#
字符串
tnkciper8#
如果你只需要显示它,我会使用number_with_precision助手。如果你在其他地方需要它,我会使用,正如Steve Weet指出的,
round
方法qqrboqgw9#
对于ruby 1.8.7,你可以在代码中添加以下内容:
字符串