Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 红宝石中的四舍五入浮点_Ruby On Rails_Ruby_Rounding - Fatal编程技术网

Ruby on rails 红宝石中的四舍五入浮点

Ruby on rails 红宝石中的四舍五入浮点,ruby-on-rails,ruby,rounding,Ruby On Rails,Ruby,Rounding,我在四舍五入方面有问题。我有一个浮点数,我想把它四舍五入到小数点的百分之一。但是,我只能使用.round,它基本上将其转换为int,意思是2.34.round。是否有一种简单的效果方法来执行类似2.3465#=>2.35显示时,您可以使用(例如) 如果要将其四舍五入存储,可以使用 >> (2.3465*100).round / 100.0 => 2.35 那么(2.3465*100).round()/100.0?将一个参数传递给round,该参数包含要舍入的小数位数 >

我在四舍五入方面有问题。我有一个浮点数,我想把它四舍五入到小数点的百分之一。但是,我只能使用
.round
,它基本上将其转换为int,意思是
2.34.round
。是否有一种简单的效果方法来执行类似
2.3465#=>2.35

显示时,您可以使用(例如)

如果要将其四舍五入存储,可以使用

>> (2.3465*100).round / 100.0
=> 2.35

那么
(2.3465*100).round()/100.0

将一个参数传递给round,该参数包含要舍入的小数位数

>> 2.3465.round
=> 2
>> 2.3465.round(2)
=> 2.35
>> 2.3465.round(3)
=> 2.347

您可以在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
    end
end
类浮点
def精度(p)
#确保精度级别实际上是一个整数且>0
引发ArgumentError,“#{p}是无效的精度级别。有效范围是大于0的整数。”除非p.class==Fixnum或p<0
#0精度的特殊情况,因此它返回一个Fixnum,因此没有尾随.0
如果p==0,则返回self.round
#标准箱
返回(自*10**p)。四舍五入到\u f/10**p
结束
结束

对于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

如果您只需要显示它,我将使用助手。
如果您在其他地方需要它,正如Steve Weet指出的那样,我会使用
舍入
方法

您也可以提供一个负数作为
舍入
方法的参数,以舍入到10、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

你可以用它来精确地四舍五入

//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          

这似乎比乘法、四舍五入和除法更明智+1Hmm ruby 1.8.7中似乎没有这种方法。“也许在1.9里?”布赖恩说。Ruby 1.8.7的舍入方法不具备这种能力,添加小数点舍入参数是1.9的能力。注意,你不会得到尾随的零,所以
1.1.round(2)
=>
1.1
不是
1.10
谢谢。我不知道sprintf会为我处理舍入<代码>sprintf“%.2f',2.3465也有效。value.round(2)比此解决方案更好请记住
2.3000.round(2)=>2.3
sprintf“%.2f',2.300=>2.30
。我认为这是round()中的一个缺陷,或者它应该有一个保留尾随零的选项。@Excalibur
2.3000。round(2)
是一个数字,而不是字符串。数字
2.3
2.30
没有区别,因此没有办法保留尾随零。您可以创建自己的具有_重要性的数字类_,但我们已经有了字符串。请注意,尽管这对小数点后两位有效,但是,
'%.3f“%1.2345
(小数点后三位,而不是2位)中存在缺陷!!sprintf也一样。当心。这将返回
=>1.234
而不是大多数人所期望的
=>1.235
(注意,在第二个小数点后,sprintf向下舍入5,只向上舍入6)。这就是何洁篪上述评论获得25票以上赞成票的原因。使用起来更安全,
'%.3f'%1.2345.舍入(3)
,因此数字首先被
.round
正确舍入,然后格式化(如果需要,使用尾随的零)。请注意,
具有精度的数字\u
是Rails唯一的方法。
# 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
//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